Hamed said:
Hi Matt,
If you want to add mystery to your puzzle try this:
char p [80];
char s [80];
p = "Hello";
s = "world";
Now it won't even compile. ;-) Let me know if this hint helps you figure out
what the problem is with your code.
That's no puzzle at all.
char p[ 80 ];
gives you an array of 80 chars. And you can't change 'p' afterwards
anymore. But that's what you're trying to do when you write
p = "Hello";
because you're treating 'p' as if it where an char pointer by trying
to assign it the address of where the (6 char long) string "Hello" is
stored (possibly in read-only memory). But 'p' isn't a char pointer,
it is an array of chars. And since these are different types the
compiler won't accept that broken code.
You might be a bit confused because when you pass 'p' to a function
what the function actually gets is a pointer to the first element of
the array 'p' (instead of a copy of the array). But that is something
special about _function calls_ with array arguments and does not
change the fact that 'p' is an array which can't be converted to a
pointer here.
And the problem of the Matt has been already explained to him, strings
like the one assigned here to 's'
char *s = "Hello";
are strings that simply can't be changed but that's what he was
trying to do, violating the rules of the game (unfortunately,
there are some implementations that let you get away with this,
making learning this more difficult).
Regards, Jens