I was not clear I guess. I am reusing a pointer by assigning different string literals to it, is that bad programming?
Ok then, suppose I am writing a function foo, and am testing it in main to see if it works. It takes a *char and does something with it. How would you do this?
Code:void foo(char *story) { ... } int main(void) { char *p = "test"; foo(p); p = "another test string"; foo(p); etcetera }
You cannot modify string literals, as you have been told over and over again. If you want to modify text you need to store them as C-style strings. See Lesson 9: C-Style Strings.
A quick example:
Code:void foo(char*); int main(void){ char myString[] = "test"; char myString2[] = "another test string"; foo(myString); foo(myString2); return (0); }
I did not express myself clearly. I am well aware that string literals cannot be changed. The function does not change them but simply takes them as an input. My point/question was, that I have the same pointer point to different string literals consecutively and whether this is correct use of the 'reused' pointer (which is a variable after all).
This line:
should cause warnings, as you are losing const-ness (the right side is "pointer to constant char", while the left side is "pointer to char").Code:char *p = "test";
You should be reading a book that dates from after the invention of "const" then.