| ARM Technical Support Knowledge Articles | |
Applies to: General Topics
Information in this article applies to:
I have defined the following initialized array of pointers:
char *array1[] = { "Text 1
", "Text 2
", "Text 3
" };
char *array2[] = { "Text 4
", "Text 5
", "Text 3
" };
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 " only one time in memory. Is this a compiler problem?
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 " and array1[1] points to "Text 2 " 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
", "Text 2
", "Text 3
" };
char const array2[][8] = { "Text 4
", "Text 5
", "Text 3
" };
With C51 you may use code instead of const.
Article last edited on: 2004-05-01 14:18:57
Did you find this article helpful? Yes No
How can we improve this article?