This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem in type casting in keil

i am having problem in type casting for lpc2129
please can any one give me an example of type casting which works,,,

  • Mike,

    are you really sure?
    Have you tested it on real hardware?
    ;)

  • sir atleast it should simulate correctly first then i will be able to test it on hardware

  • sir i know how to declare type casting;
    but alteast i want 1 example were it has been simulated correctly

  • Well, I'm much more interested in seeing any example where type casting have _not_ been simulated correctly. A failure to work correctly on the real hardware could indicate something seriously wrong with the compiler. A failure to work correctly when simulated could also indicate something seriously wrong with the simulator.

    Typecasts should always work, as long as they are valid, i.e. not breaking any alignment rule or similar. The C language standard contains information about valid type casts and the situations where the results are undefined. The compiler can't catch and warn you about casts with undefined behaviour some of them can only be found out about during runtime.

    If you have any program where a typecast doesn't work as expected, you have almost certainly missed one of the C standard paragraphs. Post a minimalistic example of your failed cast and people can probably spot why it fails.

  • main
    {
    unsigned long ii;
    func_foo(ii);
    }
    
    func_foo(unsigned long kk)
    {
    uunsigned int jj;
    jj =(unsigned int)kk;
    }
    
    


    please correct me if i m wrong

  • Your code does not do anything so there is nothing to comment about.

    If the function never makes use of jj, there is no need to perform any assign so no need to perform any type cast.

    But a more important question - what is your reason for typecasting long to int? For some architectures they have the same numeric range. For some, long will be larger. So when code uses both int and long int, neither of the types should be used unless you as developer knows that both types are good enough for what they are used for.

    If you do have code that needs to use specific data type sizes, and needs to convert between them, it's normally better to use int8_t, int16_t, int32_t and similar or maybe int_least8_t, ... or maybe int_fast8_t, ...

    In the end, you do type casts for a reason. What is your reason? And what numeric range do you have on your data? And what did you expect should happen? And what did happen?

  • ... of what "problem", exactly, you are having with typecasting!

    How do you expect people to be able to comment on a problem when you don't give any details of what the problem actually is??!

    It's like saying, "I have a problem with my car - what's wrong with it?"

  • Another thing - please use a bit modern language style.

    Write "void func_foo(unsigned long kk) { ... }" instead of just "func_foo(unsigned long kk) { ... }" if the function doesn't have a return value. If you don't give a data type, it does not mean a function without a return value. It means a function returning "int". Always be explicit and give full type definitions, so avoid the default fallback to "int".

    In C, "void" is the data type to specify for a function that doesn't return anything, and "void" is the parameter list to use for a function that doesn't take any parameters.