简体   繁体   中英

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 printf 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM