ARM Technical Support Knowledge Articles

LOCATION AND ORDER OF CONSTANTS

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

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?

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 " 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.

SEE ALSO

Article last edited on: 2004-05-01 14:18:57

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential