C: differences between char pointer and array

C: differences between char pointer and array

de Rodolfo Jalabert -
Número de respuestas: 1

Buscando cuál es la diferencia entre un puntero y un array, encontré este foro en donde se da una respuesta que me pareció que está buena:

http://stackoverflow.com/questions/1335786/c-differences-between-char-pointer-and-array

"Here's a hypothetical memory map, showing the results of the two declarations:

           0x00  0x01  0x02  0x03  0x04  0x05  0x06  0x07
0x00008000: 'n'  'o'   'w'   ' '    'i'  's'    ' '  't'
0x00008008: 'h'  'e'   ' '   't'    'i'  'm'    'e'  '\0'
    ...
amessage:
0x00500000: 'n'  'o'   'w'   ' '    'i'  's'    ' '  't'
0x00500008: 'h'  'e'   ' '   't'    'i'  'm'    'e'  '\0'
pmessage: 0x00500010: 0x00 0x00 0x80 0x00

The string literal "now is the time" is stored as a 16-element array of char at memory address 0x00008000. This memory may not be writable; it's best to assume that it's not. You should never attempt to modify the contents of a string literal.

The declaration

char amessage[]="now is the time";

allocates a 16-element array of char at memory address 0x00500000 and copies the contents of the string literal to it. This memory is writable; you can change the contents of amessage to your heart's content:

strcpy(amessage,"the time is now");

The declaration

char*pmessage ="now is the time";

allocates a single pointer to char at memory address 0x00500010 and copies the address of the string literal to it.

Since pmessage points to the string literal, it should not be used as an argument to functions that need to modify the string contents:

strcpy(amessage, pmessage);/* OKAY */
strcpy(pmessage, amessage);/* NOT OKAY */
strtok(amessage," ");/* OKAY */
strtok(pmessage," ");/* NOT OKAY */
scanf("%s", amessage);/* OKAY */
scanf("%s", pmessage);/* NOT OKAY */

und so weiter. If you changed pmessage to point to amessage:

pmessage = amessage;

then it can be used everywhere amessage can be used."