Why following lines works in keil C51 compiler & not in armcc compiler

Discussion in 'Programmer's Corner' started by aamirali, Dec 27, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    1.

    Code ( (Unknown Language)):
    1. const unsigned char me[] ={6,"my_lcd"};       // works fine in C51 compiler.
    while in armcc compiler it gives error:
    Code ( (Unknown Language)):
    1. error:  #144: a value of type "char *" cannot be used to initialize an entity of type "const uint8_t"

    2. How to get it done in armcc compiler also. I have writing decimal no in front to store how many characters a string will have


    3. Also I wrote code (this is not keil ver specific):

    Code ( (Unknown Language)):
    1.  
    2. const uint8_t *combine_data[6]        = {
    3.                                                 {"INTERPRETATION"},
    4.                                                 {"SINUS RHYTHM"},
    5.                                                 {"AXIS(QRS)NORMAL"},
    6.  
    7.                                
    8.                                         };
    9.  
    10. x = (uint8_t *)(combine_data[2] + 5);
    11. (*x)++;                                 //[B]it is not incrementing value of x here[/B]
    12.  
    13.  
     
    Last edited: Dec 27, 2012
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Ahhh...because not all compilers are implemented in the same way.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    1. Try this:


    const unsigned char[] = {4, 'H', 'I', 'Y', 'A'};

    #3 is just a mess and x is undefined anyway.
     
  4. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Why it is mess

    I have defined
    uint8_t *x;
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    You are defining the object named "me" to be a pointer to an array of values of unsigned char with the size of the array being established by the initialization vector provided.

    You initialization vector consists of two values, the number 6, which is compatible with being stored as an unsigned char, and an expression, "my_lcd", that evaluates to the address of the first character in a NUL terminated string that is stored as part of the program code. This is NOT compatible with being stored as an unsigned char. Unless the standard (assuming the compiler claims to even be standards-compliant) defines this as illegal syntax, it invokes either unspecified or undefined behavior (probably undefined, in this case) which a given compiler is free to define however it chooses.


    Your C51 compiler may have chosen to use that notation as a shortcut way of initializing the values of a char array, but I would do some tests to be sure that it is really doing what you think it is. Just because it compiled and didn't throw an error or warning does not mean that it did what you wanted.
     
Loading...