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
  • 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?

    The 'C' language does not do array bounds checking!

    You need something like lint if you want to check you code for problems like this.

    Technically, [] is an operator in 'C'; it simply indexes relative to a pointer - any pointer - it is not constrained to arrays!

    Thus display[400] simply refers to the location 400 bytes after the location pointed-to by display; the compiler just assumes that you knew what you were doing when you asked it to do that

Reply
  • 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?

    The 'C' language does not do array bounds checking!

    You need something like lint if you want to check you code for problems like this.

    Technically, [] is an operator in 'C'; it simply indexes relative to a pointer - any pointer - it is not constrained to arrays!

    Thus display[400] simply refers to the location 400 bytes after the location pointed-to by display; the compiler just assumes that you knew what you were doing when you asked it to do that

Children
No data