2

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?

4
  • What is it displaying? Commented Aug 27, 2016 at 15:16
  • 2
    You might want to spend a little time looking at the printf and format string documentation. Commented Aug 27, 2016 at 15:18
  • You have a 1D-string array, which in fact is a 2D-char array. (I adjusted the title accordingly) Commented Aug 27, 2016 at 15:25
  • Sorry for bothering you guys with such na amateur mistake. I edited the title to make it match with the text. Thank you for the answers! Commented Aug 27, 2016 at 16:11

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.