I'd prefer to say that a pointer can change its string literal. I wouldn't really say that, it sounds awkward, but the point is that string literal should be pointed to, it doesn't own it's own pointer and the data itself doesn't really decay into a pointer. A string literal is actually "This" or "this" -- objects of type char[5] -- the data proper.
char *p = "This";
p can be reassigned, because it is a pointer. And that very syntax is only allowed as a convenience. A convenience which C++ copied from the beginning, I think, but nonetheless. Before C was standardized, you needed to do magic like this:
char * p;
char cs[] = "This";
p = cs;
I will even link a FAQ answer later that shows the difference between a pointer pointing to an array object and an array itself with pictures.
This code that you posted is actually guaranteed not to work.
Arrays cannot be reassigned, because that's illegal. If arrays were pointers, then it would be legal. str++; is:
str = str + 1;
which means that this requires array assignment. With a pointer, this operation is fine, though it's not guaranteed to point to a dereferenceable location. In other words, you can do this:
But you cannot increment foo here, because foo is an array type. Try, and you will get that error. foo is the wrong type to increment.Code:#include <stdio.h> #include <ctype.h> int main(void) { char foo[] = "My sample string."; for (char *bar = foo; *bar != '\0'; bar++) { *bar = toupper(*bar); } printf("%s\n", foo); return 0; }
Try reading this FAQ answer.
Last edited by whiteflags; 02-15-2013 at 01:01 AM.
The more you know something, the easier you can explain. I believe that you have a good knowledge, so rephrase what you said.
Ι believe I said that too.
That is what I am trying to say... but thanks for the FAQ, since I didn't know about this page.
Exactly! The code I posted was for demonstration of which you can do and what you can't! I thought this was clear by the printfs
However, I like your example, so I am going to augment my upload on my page with your example and the faq link. Of course you will get the credits
EDIT: Here it is.
Last edited by std10093; 02-15-2013 at 06:52 AM.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
I hope you are doing this because you didn't understand me somehow.
String literals have a type. It's array of char. And while you said "String literals can not modify their data, but they can modify their pointer," my main problem with this is that they don't have a pointer like you implied. The pointer and the string literal are actually separate things.
Please. I give 0 ........s about credit here.Ι believe I said that too.
That is what I am trying to say... but thanks for the FAQ, since I didn't know about this page.
Exactly! The code I posted was for demonstration of which you can do and what you can't! I thought this was clear by the printfs
However, I like your example, so I am going to augment my upload on my page with your example and the faq link. Of course you will get the credits
If you were trying to say what I said, I felt I needed to rephrase your post anyway. You kept referring to arrays as pointers. Arrays are not pointers. And I thought it was important enough to say that arrays in general can't be reassigned, and it's not just a string literal issue.