13

Possible Duplicate:
What is the difference between char s[] and char *s in C?

Why is:

char *ptr = "Hello!"

different than:

char ptr[] = "Hello!"

Specifically, I don't see why you can use (*ptr)++ to change the value of 'H' in the array, but not the pointer.

Thanks!

Community
  • 1
  • 1
John McGee
  • 171
  • 1
  • 1
  • 4

5 Answers5

41

You can (in general) use the expression (*ptr)++ to change the value that ptr points to when ptr is a pointer and not an array (ie., if ptr is declared as char* ptr).

However, in your first example:

char *ptr = "Hello!"

ptr is pointing to a literal string, and literal strings are not permitted to be modified (they may actually be stored in memory area which are not writable, such as ROM or memory pages marked as read-only).

In your second example,

char ptr[] = "Hello!";

The array is declared and the initialization actually copies the data in the string literal into the allocated array memory. That array memory is modifiable, so (*ptr)++ works.

Note: for your second declaration, the ptr identifier itself is an array identifier, not a pointer and is not an 'lvalue' so it can't be modified (even though it converts readily to a pointer in most situations). For example, the expression ++ptr would be invalid. I think this is the point that some other answers are trying to make.

Salek
  • 449
  • 1
  • 10
  • 19
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    And finally, someone answered my question. If I had a registered account, I would give you an upvote. Thank you kind sir. – John McGee Jul 08 '10 at 20:02
  • 8
    @John McGee: That you posted a question means you have an "account", you have enough reputation to upvote, and you always can mark an answer as "accepted". – jamesdlin Jul 08 '10 at 20:11
  • Im too lazy to try out right now, but would (*ptr)++ in the first case really result in an compiler error? The compiler would need to analyse if ptr hasn't changed since initialization, wouldn't he? – MartinStettner Jul 08 '10 at 20:15
  • @Martin: it won't generally result in a compile-time error (maybe a warning if your compiler is smart and can detect that `ptr` points to a literal), but it will often result in a runtime error. However, it might appear to work on some platforms - but it's undefined behavior and should be regarded as a bug on any platform. – Michael Burr Jul 08 '10 at 20:19
  • 1
    @Michael: Thanks. Not to mention the cases when constant strings are shared (i.e. reused), which is much more common I think ... – MartinStettner Jul 08 '10 at 20:26
  • @jamesdlin: It won't let me upvote until I register. I just tried. – John McGee Jul 08 '10 at 20:38
  • To be pedantic, `ptr` is an lvalue in some cases, like when it is the operand of `sizeof` or `&`: https://stackoverflow.com/questions/39021998/is-there-a-reason-why-an-array-name-is-not-an-lvalue – jinawee Apr 22 '19 at 09:36
2

When pointing to a string literal, you should not declare the chars to be modifiable, and some compilers will warn you for this:

char *ptr = "Hello!"    /* WRONG, missing const! */

The reason is as noted by others that string literals may be stored in an immutable part of the program's memory.

The correct "annotation" for you is to make sure you have a pointer to constant char:

const char *ptr = "Hello!"

And now you see directly that you can't modify the text stored at the pointer.

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
0

Arrays automatically allocate space and they can't be relocated or resized while pointers are explicitly assigned to point to allocated space and can be relocated.

Array names are read only!

amfeng
  • 1,065
  • 5
  • 17
  • Thanks, but it doesn't answer my question. I am asking why (*ptr)++ for the array will change the 'H' to an 'I', but not for the other case. – John McGee Jul 08 '10 at 19:57
0

If You use a string literal "Hello!", the literal itself becomes an array of 7 characters and gets stored somewhere in a data memory. That memory may be read only.

The statement

char *ptr = "Hello!";

defines a pointer to char and initializes it, by storing the address of the beginning of the literal (that array of 7 characters mentioned earlier) in it. Changing contents of the memory pointed to by ptr is illegal.

The statement

char ptr[] = "Hello!";

defines a char array (char ptr[7]) and initializes it, by copying characters from the literal to the array. The array can be modified.

Maciej Hehl
  • 7,895
  • 1
  • 22
  • 23
0

in C strings are arrays of characters. A pointer is a variable that contains the memory location of another variable. An array is a set of ordered data items. when you put (*ptr)++ you are getting Segmentation Fault with the pointer.

Maybe you are adding 1 to the whole string (with the pointer), instead of adding 1 to the first character of the variable (with the array).

pampanet
  • 131
  • 6