0

Although the below code is working fine.But its showing "segmentation fault" when i am trying to accept the strings using fgets as i want to store strings which might contain spaces.How to accept strings (which might contain spaces) in character pointer array

int main(){
    char* nm[5];
    char* st;
    for(int i=0;i<5;i++)
        //fgets(nm[i],30,stdin);
        scanf("%ms",&nm[i]);
    for(int i=0;i<5;i++)
        printf("%s\n",nm[i]);
return 0;
}
NIKITA AGARWAL
  • 141
  • 1
  • 7

1 Answers1

-1

nm is a pointer to 5 chars not a pointer to 5 strings, so &nm[2] is the address of the 3rd char in nm. If you change nm to be

char* nm[5][50];

you will have an array of 5 x 50 chars. That should stop the segfault.

Neil
  • 11,059
  • 3
  • 31
  • 56