C embedded questions (4): What is the difference between strings and character arrays?

What is the difference between strings and character arrays? 

A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicitly specified by using the static keyword.
Actually, a string is a character array with following properties:
* the multi byte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area. For example, in the following declarations:


char *s1 = “Perl has a strong linkage with PHP”;
char *s2 = “Perl has a strong linkage with PHP”;

The strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following:

char ca1[] = “Perl has a strong linkage with PHP”;
char ca2[] = “Perl has a strong linkage with PHP”;

Etichete

Afișați mai multe

Arhiva

Afișați mai multe