-1

hi Iam new to programming i have a problem in reading a multiple strings can anyone help me.

    #include<stdio.h>
    #include<string.h>
    int main()
    {
       int a;
       char n[50];
       scanf("%d",&a);
       for(int i=0;i<a;i++)
       {
          scanf("%s",n[i]);
       }

       for(int i=0;i<a;i++)
       {        
          printf("%s\n",n);
       }

       return 0;

    }
Tormund Giantsbane
  • 358
  • 1
  • 4
  • 12
SAI KUMAR
  • 1
  • 1
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 28 '18 at 19:09
  • [How do I create an array of strings in C?](//stackoverflow.com/q/1088622) – Johnny Mopp Mar 28 '18 at 19:16
  • 1
    I think the compiler already helped you by emitting a warning. Didn't it? – Arndt Jonasson Mar 28 '18 at 19:16
  • 1
    What makes you think that you have a problem? Compiler error? Quote it. Misbehaviour? Describe it. Crash? Debug it. – Yunnosch Mar 28 '18 at 19:20
  • There are some very relevant comments here so far, and you should heed them, but few address your immediate issue. (which could have been satisfied by a little better search before asking here) There are several viable answers addressing your question _[HERE](https://stackoverflow.com/questions/314401/how-to-read-a-line-from-the-console-in-c)_. – ryyker Mar 28 '18 at 19:30
  • @Yunnosch So basically there's never a time when asking for help is the right choice? :) – unwind Mar 28 '18 at 19:50
  • n[i] is not a string, so reading with %s into n[i] is wrong. – FredK Mar 28 '18 at 20:03
  • @unwind It is not about the time. It is about how unnecessarily hard you make helping you. Yell "Help!" or explain what information you have on your problem. What do you think might be the quicker way? – Yunnosch Mar 28 '18 at 20:54

2 Answers2

0

The value of 'a' must be (total length of string)+1 and always less than max size of array. In first for loop:

     scanf("%s",&n[i]);

If you use scanf then while entering input you need to press [Enter] after each letter. Using getchar() instead of that scanf will let you type the entire string at once. Use this instead of scanf:

    n[i]=getchar();

Lastly, that second for loop is not needed,

    printf("%s\n",n); 

alone will print the entire string.

VH97
  • 13
  • 4
0

You have to define 2-D array not just 1-D

char n[number_of_strings][each_string_length]

Be careful of your buffer overflow too. Using scanf() is not safe, you should limit your string length by not exceeding.

each_string_length-1

Strings in C have a '\0' null terminator to let compiler recognize it as a string not just an array of characters. That is why you should substract one from string length which is reserved for nul terminator