In article <sl************************@blackbart.mynetwork> ,
Tyler Smith <ty*********@mail.mcgill.cawrote:
>I have a very basic question. What is the difference between
char *string = "This is a string";
and
char string[] = "This is a string";
The first declares "string" as a pointer; it's initialised to point to
(the first character of) the string "This is a string". If that same
string is used elsewhere in the program, the compiler is allowed to
just keep one copy, and that wouldn't work if the string was
modifiable, so compilers are allowed to keep the string in read-only
memory. You can change the variable to point at some other string,
but you can't change the characters in the initial string.
The second declares "string" as an array of characters - not a
pointer, but as an array itself. The array is initialised to contains
the string "This is a string", and this is (so to speak) the
variable's own memory and you can modify it.
strtok() modifies the string you pass to it, so you can't use a
read-only string such as the one in your first case.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.