Is it valid to take the address of a string literal, or to iterate over it?

Viewed 113

It is certainly valid to do this:

char src[] = "Allie has a cat.";
char buff[20];
strcpy(buff, src);
printf("%s\n", buff);

Or this:

printf("Allie has a cat.\n");

But, is it valid to shorten the above code to this?

char buff[20];
strcpy(buff, "Allie has a cat.");
printf("%s\n", buff);

Or to write:

printf("%s", "Allie has a cat.");

The reason I’m asking is that AFAIK in C string literals are somewhat different animals than character arrays, and while both examples seem to be working (ideone#1, ideone#2), I think that when it comes to C I should not do anything I only suppose should be valid or just seems to be working b/c of the number of pitfalls this language has and how easy it is in this language to write a construct that looks innocent but nevertheless is UB.

2 Answers
Related