Rafael Kehl Rafael Kehl - 1 year ago 69
C Question

Initialize a 2D string array and print it

I'm learning C for fun and I'm trying to make a program that creates a array of strings. I'm trying this right now:

char str[2][10];
strcpy(str[0], "foo");
strcpy(str[1], "bar");
printf("%d \n",str[0]);
printf("%d \n",str[1]);

But my
returns only some numbers, and I would like it to display foo and bar. I've been reading about and I think that it is displaying a pointer to my strings. What am I doing wrong?

Answer Source

You are printing using the format %d, which is used to print an int. Modify your code to :

printf("%s\n", str[0]);
printf("%s\n", str[1]);

See this link for more information on the specifiers' format for printf.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download