"C Wood" <> wrote in message
news:LDlKa.12831$...
>
> Dear group,
>
> I forgot which declaration is constant. Intentions are:
>
> void some_func() {
> char temp[] = "prepend"; /*This one*/
> char *temp= "prepend"; /*Or this one*/
> strcat(temp," before this");
> }
>
> Thanks...
>
The second, because all you've declared is a pointer which points at a
string literal, and string literals are read-only.
Despite appearances the first doesn't involve a string literal, its a
confusing short hand for this.
char temp[] = {'p', 'r', 'e', 'p', 'e', 'n', 'd', '\0' };
Obviously if you declare a non-const array, you can modify it.
Your strcat is illegal in both cases, nothing to do with const, instead you
are writing over memory you haven't allocated in any way.
john
|