Re: C Strings



On Mar 4, 7:01 am, Beej Jorgensen <b...@xxxxxxx> wrote:
Mark McIntyre <markmcint...@xxxxxxxxxxx> wrote:

I was thinking of string literals, which aren't arrays, but point
taken and pun intended.

I thought string literals were arrays until you operated on them, at
which point they became char*s?

#include <stdio.h>

int main(void)
{
char *p1;
char (*p2)[];

p1 = "foobar";
p2 = &"foobar";

p1 = &"foobar"; /* invalid */
p2 = "foobar"; /* invalid */

printf("%zu\n", sizeof("foobar")); /* "7" */
printf("%zu\n", sizeof("foobar"+0)); /* "4" */

return 0;

}

-Beej

Here consider the two lines:
printf("%zu\n", sizeof("foobar")); /* "7" */
printf("%zu\n", sizeof("foobar"+0)); /* "4" */

What is %z?
What is the effect of adding 0 to "foobar" ?

The reason for asking these questions is that VC++2005 does not
recognize %z (In K&R 2nd edition also, I could not find %z). Also,
when I have %u instead of %zu, VC++ 2005 prints 7 and 7 for both
printf statements.

.



Relevant Pages