-1

I was trying to display array of strings given by user. I used char to declare string and for loop for input and display. Can anyone tell me what is wrong with the code.

#include <stdio.h>
#include <string.h>

int main()
{ 
  int n,i;
    scanf("%d",&n);
    strings st[n];
    
    for( i = 0; i<n ; i++){
        scanf("%s",st[i]);
    }
    for( i = 0; i<n ; i++){
        printf("%s \n",st[i]);
        
    }

}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85

4 Answers4

0

regarding:

for( i = 0; i<n ; i++)
{
    scanf("%s",st[i]);

before this call to scanf() need to allocate memory for the st[i] pointer to be pointing at. perhaps via

st[i] = malloc( 1024 );  
if( ! st[i] ) 
{ 
    perror( "malloc failed" ); 
}  

else 
{ 
    if( scanf("%1023s",st[i]) != 1 ) 
    { 
        fprintf( stderr, "scanf failed\n" ); 
    } 
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

In C, you have to do it like this:

#include <stdio.h>

int main()
{ 
  int n,i;
    scanf("%d",&n);
    char st[n][100]; // Line 7
    
    for( i = 0; i<n ; i++){
        scanf("%s",st[i]);
    }
    for( i = 0; i<n ; i++){
        printf("%s \n",st[i]);
        
    }
}

This program can read strings up to 100 characters long. To change this limit, modify the value in the second bracket in line 7.

Second, there is no such type as strings in C. If you've seen this type on the internet, you've most likely been looking at C++ code. C++ and C are different programming languages - they're not the same. If you'd like to do it in C++ leave a comment and I'll show you how to do it.

Gabor Szita
  • 319
  • 4
  • 13
  • 1
    "This program can read strings up to 100 characters long" is true if the length includes the _null character_, else it should be "up to 99". Better to use `"%99s"` than `"%s"` here. – chux - Reinstate Monica Mar 02 '21 at 18:15
0

this is the solution for your problem but try to see more tutorials about c

 #include <stdio.h>
int main()
{
  int n,i;
    scanf("%d",&n);
   char st[n][50];

    for( i = 0; i<n ; i++){
        scanf("%50s",st[i]);
    }
    for( i = 0; i<n ; i++){
        printf("%s \n",st[i]);

    }

}
fares
  • 97
  • 1
  • 5
  • #include is not needed. It also doesn't work in plain C code. (string.h is only for C++) – Gabor Szita Mar 02 '21 at 17:07
  • 1
    `scanf("%s",st[i]);`, without a width limit, is bad like [gets()](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – chux - Reinstate Monica Mar 02 '21 at 18:12
  • Also, check the return value from `scanf("%d",&n);` and the other `scanf` – Ed Heal Mar 02 '21 at 20:33
  • width limit fixed . #include was a fault from previous program but #include is work for c check this link [link](https://fresh2refresh.com/c-programming/c-function/string-h-library-functions/) – fares Mar 02 '21 at 20:33
0

A more dynamic approach using array of char *

#include <stdio.h>
#include <stdlib.h>

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#define LINESIZE 255
#define SCAN_LINE " %" STR(LINESIZE) "[^\n]"

int main() {
    int n,i ;
    char *buf;
    char (*lines)[LINESIZE];
    printf("Enter number of lines: ");
    scanf("%d",&n);
    buf  = malloc(n*sizeof(lines));

    lines = (char (*)[])buf;

    for( i = 0; i<n ; i++){
        scanf(SCAN_LINE,lines[i]);
    }
    for( i = 0; i<n ; i++){
        printf("%s \n",lines[i]);

    }
}
jordanvrtanoski
  • 5,104
  • 1
  • 20
  • 29