Originally Posted by
localrob
That worked great, thanks
I'm a little confused as to where the strings are stored if not in an array of char, but it works.
They are string literals, stored in a string table, unmodifiable by you, and if you change the pointer, lost to you. Example:
Code:
char *s = "foo";
s = "bar";
Since s is simply a pointer, non-constant, we're allowed to change where it points here. It first points to the string literal "foo. We then change what it points to, by making it point at another string literal, "bar". The first literal is now lost to us, and we have no way of finding it again, because we have nothing pointing at it any more.
While we cannot also modify either of the literals. They're likely to be stored in a read-only portion of the program, and as such, by standard, are not allowed to modify them. For instance, we cannot do:
Code:
s = "bar";
*s = 'c';
This will, or should, generate a runtime error for you. You're not allowed to modify them.
Basicly, string literals, at compile time, are all "seen" by the compiler, and stored in a table of strings which are then pointed to by whatever is trying to use them. Sort of like:
Code:
char *stringtable[] =
{
"this is a literal",
"this is another",
"same here",
};
...
void foo( void )
{
char *s = stringtable[2];
printf("%s", s; );
}
It sort of ends up like that.
Quzah.