Keil Logo

GENERAL: LOCATION AND ORDER OF CONSTANTS


Information in this article applies to:

  • C51 All Versions
  • C166 All Versions
  • C251 All Versions

QUESTION

I have defined the following initialized array of pointers:

char *array1[] = { "Text 1\n", "Text 2\n", "Text 3\n" };
char *array2[] = { "Text 4\n", "Text 5\n", "Text 3\n" };

I see in the debugger that the stings are not located in the same order as I have defined them and I can find "Text 3\n" only one time in memory. Is this a compiler problem?

ANSWER

No, in this case the compiler may change the order of the strings and may remove identical strings. You have defined arrays of pointers which are initialized with addresses of constant strings. Even if the order of the strings in memory is exchanged, the pointer arrays are still correct. For example array1[0] points to "Text 1\n" and array1[1] points to "Text 2\n" even if these strings are located in reverse order. On the other hand, identical strings are only generated once and multiple pointers use its address.

If the order of the strings is important, you may use a multi-dimensional array instead:

char const array1[][8] = { "Text 1\n", "Text 2\n", "Text 3\n" };
char const array2[][8] = { "Text 4\n", "Text 5\n", "Text 3\n" };

With C51 you may use code instead of const.

SEE ALSO

Last Reviewed: Saturday, May 1, 2004


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.