1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

   char *s1[200][200];
   fgets( *s1[0][0] , 100 , stdin);

   fgets( *s1[0][1] , 100 , stdin);
   fgets( *s1[0][2] , 100 , stdin);
   fgets( *s1[0][3] , 100 , stdin);



   printf("%s  %s  %s  %s",&s1[0][0],&s1[0][1],&s1[0][2],&s1[0][3]);

}

i want to use array of strings to store strings in multidimensional array but it can't store words with more than 3 characters what's wrong and how can i make it work with strings with 100 characters

ahmadi
  • 11
  • 1

1 Answers1

0

It seems you've gotten confused about pointers, and then tried to add and remove * until the compiler let you get away with it. But this is not doing what you think it's doing.

You want a 200x200 arrayof up to 100-character strings. The way you have currently written it, you would need to allocate memory for each string before you read it and then store the pointer in your array. Or read it into a larger buffer first and then allocate just enough memory, copy the string and store. This might actually be a good idea.

But by far the easiest fix is to allocate a 200x200 array of 100 characters. i.e.

char s1[200][200][100] = {0};

for( int i = 0; i < 4; i++ ) {
    fgets( s1[0][i], 100, stdin );
}

printf( "%s  %s  %s  %s", s1[0][0], s1[0][1], s1[0][2], s1[0][3] );
paddy
  • 60,864
  • 6
  • 61
  • 103