0

I am unable to print an array of strings after taking them as input from user.

  int main()
{
   char name[3][10];

   for (int i=0;i<3;i++)
     { printf("name:\n");
       scanf("%9[^\n]",&name[i][0]);
     }

   for (int i=0;i<3;i++)
      printf("%s\n",&name[i][0]);

}

Although this works perfectly fine (without the loop)

    int main()
 {   
     char name[2][10];

     printf("enter the name");
     scanf("%9[^\n]",&name[0][0]);

     printf("%s",&name[0][0]);
 }

Please help me as to where am I mistaken.

  • `scanf("%9[^\n]",&name[i][0]);` --> `scanf(" %9[^\n]", &name[i][0]);` – BLUEPIXY Oct 27 '17 at 14:51
  • 1
    Btw, `&name[i][0]` is just a convoluted way of writing `name[i]`. –  Oct 27 '17 at 14:51
  • C'mon now everyone... if you want to answer the question, actually post an answer. "Use comments to ask for more information or suggest improvements. Avoid answering questions in comments." – Andrew Cottrell Oct 27 '17 at 14:53
  • 3
    @AndrewCottrell it's the "same old `scanf()` question" (with minor variations, all involving newlines / whitespace-skipping) over and over again ... Someone find a good dupe for this please. –  Oct 27 '17 at 14:56
  • 1
    @Random_Girl: You might be interested in my [beginners' guide away from `scanf()`](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) (you can skip some passages I guess, but some are relevant). –  Oct 27 '17 at 14:57
  • 1
    @AndrewCottrell I think that's good enough, thanks. –  Oct 27 '17 at 15:11
  • @FelixPalmen: I just read that scanf() guide you posted. Excellent work! – Andrew Cottrell Oct 27 '17 at 15:34
  • @FelixPalmen That guide is really fantastic. Thanks! Have you made more such guides? – Random_Girl Oct 27 '17 at 19:33
  • @Random_Girl Well thank you ;) This is the only one so far, but I'm thinking about another one on the topic of pointers and arrays, as this is a canonic source for beginners' confusion as well. –  Oct 28 '17 at 11:04

0 Answers0