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 with character pointer

Hello,

1) I am declaring a pointer to a string as
Xdata Unsigned char *sTemperature [4] = {"NIL","25 ","30 ","37 "};

This is shown in the .lst file as a array occupying only 12 bytes where as it should take 16 bytes according to me (4 bytes for NULL characters also) Why is it so?

Also if I give the statement:
STemperature[2][2] = 'C';
'C' is not appended after 25 in the program. What is wrong?

2) If I declare a array as:
Unsigned char display[40];
Unsigned char sBlankString = " "; /* say a string of 40 blank characters */

And give a statement
Strncpy(&display[400],sBlankString,strlen(sBlankString));

Shouldn't the compiler give a warning message as we are copying at display[400] where as the
Display is defined only for 40 characters?


Regards,

Mohit

Parents
  • 1) I am declaring a pointer to a string as
    Xdata Unsigned char *sTemperature [4] = {"NIL","25 ","30 ","37 "};

    This is shown in the .lst file as a array occupying only 12 bytes where as it should take 16 bytes according to me (4 bytes for NULL characters also) Why is it so?

    You see the size of an array of pointers to characters. The compiler places 4 pointers in the array, each with a size of 3 bytes (generic pointer).

    Also if I give the statement:
    STemperature[2][2] = 'C';
    'C' is not appended after 25 in the program. What is wrong?


    STemperature[2] selects the 3rd pointer in the list which points to the first char of "30". The 'C' is placed behind the '0'. This overwrites the terminating 0x0 of the string which may give you strange results. There is no room for appending characters. You have to copy the string elsewhere and then append characters. Example:

    char cString [16]; /* this allows 15 characters + 0x0 */
    strcpy ( cString, STemperature[1] ); /* copy "25" */
    strcat ( cString, "C" ); /* Add 'C' to "25" */ 

    2) If I declare a array as:
    Unsigned char display[40];
    Unsigned char sBlankString = " "; /* say a string of 40 blank characters */

    And give a statement
    Strncpy(&display[400],sBlankString,strlen(sBlankString));


    &display[400] is a pointer to an unsigned char which is passed as an argument to a function. I don't think the compiler has to check the contents of a parameter. Your variable can be preset with
     memset ( display, ' ', sizeof(display)); /* fill with spaces */
    This works always, independent of the size of the variable.

    HHK

Reply
  • 1) I am declaring a pointer to a string as
    Xdata Unsigned char *sTemperature [4] = {"NIL","25 ","30 ","37 "};

    This is shown in the .lst file as a array occupying only 12 bytes where as it should take 16 bytes according to me (4 bytes for NULL characters also) Why is it so?

    You see the size of an array of pointers to characters. The compiler places 4 pointers in the array, each with a size of 3 bytes (generic pointer).

    Also if I give the statement:
    STemperature[2][2] = 'C';
    'C' is not appended after 25 in the program. What is wrong?


    STemperature[2] selects the 3rd pointer in the list which points to the first char of "30". The 'C' is placed behind the '0'. This overwrites the terminating 0x0 of the string which may give you strange results. There is no room for appending characters. You have to copy the string elsewhere and then append characters. Example:

    char cString [16]; /* this allows 15 characters + 0x0 */
    strcpy ( cString, STemperature[1] ); /* copy "25" */
    strcat ( cString, "C" ); /* Add 'C' to "25" */ 

    2) If I declare a array as:
    Unsigned char display[40];
    Unsigned char sBlankString = " "; /* say a string of 40 blank characters */

    And give a statement
    Strncpy(&display[400],sBlankString,strlen(sBlankString));


    &display[400] is a pointer to an unsigned char which is passed as an argument to a function. I don't think the compiler has to check the contents of a parameter. Your variable can be preset with
     memset ( display, ' ', sizeof(display)); /* fill with spaces */
    This works always, independent of the size of the variable.

    HHK

Children
No data