2

Trying to store input into array of strings with following code:

#include <stdio.h>
#include <string.h>
int main()
{
    int noOfStrings;
    printf("Enter no of strings: ");
    scanf("%d", &noOfStrings);
    char *string[noOfStrings];
    for(int i=0; i<noOfStrings; i++){
        printf("\nEnter string %d: ", i);
        scanf("%s\n",string[i]);
    }
    return 0;
}
-----------------------------------------------------------------------------
Console:
Enter no of strings: 3                                                                                                   

Enter string 0: abc                                                                                                      

Enter string 1: def                                                                                                      

Enter string 2: ghi                                                                                                      

Segmentation fault (core dumped)                                                                                         


...Program finished with exit code 139                                                                                   
Press ENTER to exit console.
-----------------------------------------------------------------------------

I am not able to figure out why this is failing.

I also Tried following code with fixed size array.

#include <stdio.h>
#include <string.h>
int main()
{
    int noOfStrings;
    printf("Enter no of strings: ");
    scanf("%d", &noOfStrings);
    char string[noOfStrings][5];
    for(int i=0; i<noOfStrings; i++){
        printf("\nEnter string %d: ", i);
        scanf("%s\n",string[i]);
    }
    printf("\nPrinting Stored Strings");
    for(int i=0; i<noOfStrings; i++){
        printf("\nEnter string %d: ", i);
        printf("%s\n",string[i]);
    }
    return 0;
}
-----------------------------------------------------------------------------
Console:
Enter no of strings: 3                                                                                                 

Enter string 0: abc                                                                                                    
def                                                                                                                    

Enter string 1: ghi                                                                                                    

Enter string 2: jkl                                                                                                    

Printing Stored Strings                                                                                                
Enter string 0: abc

Enter string 1: def                                                                                                    

Enter string 2: ghi                                                                                                    


...Program finished with exit code 0                                                                                   
Press ENTER to exit console. 

After entering 1st string ('abc') There was no prompt for 2nd string, so proceeded by entering 'def'. Followed by 2 more strings. Notice that string 'jkl' is not printed.

Please tell me what i am missing in these 2 cases?

Thanks.

8
  • 2
    And in first case you declare array of pointers and each pointers needs to be allocated memory before dereferencing . Commented Feb 7, 2019 at 20:10
  • @xing I tried removing the \n in both cases I got the following outputs: Case 1- Enter no of strings: 3 Enter string 0: abc def Enter string 1: ghi Enter string 2: jkl Printing Stored Strings Enter string 0: abc Enter string 1: def Enter string 2: ghi Case 2- Enter no of strings: 3 Enter string 0: abc def Enter string 1: ghi Enter string 2: jkl Printing Stored Strings Enter string 0: abc Enter string 1: def Enter string 2: ghi Commented Feb 7, 2019 at 20:22
  • 1
    Every time you read user input with *scanf() and don't check the return value, that's relying on the user to not invoke undefined behaviour of your program. Don't do that. Commented Feb 7, 2019 at 20:24
  • 1
    @DevSolar Understood :) Commented Feb 7, 2019 at 20:37
  • 1
    @xing Error from my side suggested change fixed the issue. Thanks Commented Feb 7, 2019 at 20:44

1 Answer 1

2

In the first case, you're defining an array of pointers:

char *string[noOfStrings];

However, these pointers are uninitialized. When you then attempt to use scanf, you dereference these invalid pointers. Doing so invokes undefined behavior, which in this case causes a crash.

Your second case fixes this by using a 2D array of characters big enough to hold the strings you input. But you get stuck because of your scanf format:

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

The \n in the format string matches any number of whitespace characters, so the function won't return until you enter a non-whitespace character. You can fix this by removing \n from the format string.

scanf("%s",string[i]);
Sign up to request clarification or add additional context in comments.

4 Comments

I tried removing the \n in both cases I got the following outputs: Case 1- Enter no of strings: 3 Enter string 0: abc def Enter string 1: ghi Enter string 2: jkl Printing Stored Strings Enter string 0: abc Enter string 1: def Enter string 2: ghi Case 2- Enter no of strings: 3 Enter string 0: abc def Enter string 1: ghi Enter string 2: jkl Printing Stored Strings Enter string 0: abc Enter string 1: def Enter string 2: ghi
@sdnemina Aside from the poor formatting in the comments, that looks right. How does that differ from what you expect?
Notice that i had to enter def before "Enter string 1" and also had to enter 'jkl' in the output it shows Printing Stored Strings Enter string 0: abc Enter string 1: def Enter string 2: ghi . Also apologies about the formatting as well as using 'Enter String' while printing the outputs too.
@dbug Error from my side , the suggested change fixed the issue. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.