Jose wrote:
If you assign a string constant to a character array, say
char strg[11] = "C rules!!!"; the compiler automatically assigns each character of the string constant (wich is really an array of characters too) to the array ( yeah I know, it can also be char strg[] = "C rules!!!"
Now what's confusing is that C treats the strings diferently if they're assigned to a character pointer, for example char *strg = "C rules!!!"; in this case it just assigns a pointer to the string constant.
OK, either way the string is stored as a sequemce of bytes.
So you have "C rules!!!0x00" Where 0x00 is the terminating byte which is always zero.
To use that string in your code you need a way to get an address to the start of it.
If you create it as an array, the address is the array (an array is a pointer to the first element in the array).
This value is constant and you cannot change it so it will always point to the start of the data (though you may change the data unless it is const).
If you use the strng* = "C rules!!!" version then you still have a pointer to the string but you CAN change where this pointer points to so you could lose the start of your string if you mess with the pointer. Sometimes you don't care.
If you have a string that you only wish to print out, for e.g. you can printf("Hello World\n"); which the compiler will turn into the array "Hello World\n" stored somewhere in memory and will generate a call for printf that says printf(char * pointerToString = "Hello World\n") more or less.
If you want to have the start of the string for many uses you can create the string in an array and repeatedly re-use it e.g.
char string[]="Hello World\n";
printf(string);
printf(string);
since the array is just a pointer (well, actually it's an address but we'll ignore the distinction right now for C).
You can even:
char string[] = "Hello World\n";
char* stringPtr = string;
printf(stringPtr);
/* but you can also change what the pointer points to */
stringPointer ++;
printf(stringPtr);
Which would give you the output:
Hello World
ello World