Scanf parsing string input into array of chars

20,493

Solution 1

You haven't allocated memory for your strings. The arguments you give to scanf are uninitialized pointers.

top[i] = "test" assigns a pointer to your variable and initializes it with a valid value.

In contrast, scanf(..., top[i]) tries to write to where top[i] points. But top[i] isn't initialized and points to some random location, which results in your memory access error.

When you look at man scanf, you can read under

Conversions
...
s
Matches a sequence of non-white-space characters;

and now the important part

the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically.

So you must allocate an array via malloc() or declare the character array large enough.

char top[10][21];
char bottom[10][21];
int i;
for(i = 0; i < 5; i++){
    printf("Karte %d: Obere Werte? ", i);
    scanf("%20s",top[i]);
    printf("Karte %d: Untere Werte? ", i);
    scanf("%20s",bottom[i]);
}

With

scanf("%20s",top[i]);

you limit the number of characters read, to prevent a buffer overrun

Solution 2

As another answer says, you have to allocate place for your strings. You want an array of top and bottom strings, but with char * top[10] you are only allocating room for 10 pointers, not for the characters in the strings. The reason why top[10] = "Hello" works is that string literals are implicitly allocated by the compiler, which returns a pointer to those characters.

char *top[10], *bottom[10];
for(i = 0; i < 5; i++)
{
    top[i] = malloc(100); // reserve room for string up to 100 chars
    printf("Karte %d: Obere Werte? ", i);
    scanf("%s",top[i]);
    bottom[i] = malloc(100);
    printf("Karte %d: Untere Werte? ", i);
    scanf("%s",bottom[i]);
}
Share:
20,493
Fabian
Author by

Fabian

Hey, I am Fabian from Aachen, Germany, and a freelance fullstack web developer :) Looking for interesting projects in Aachen, Düsseldorf, Cologne or remotely world-wide to bring web app ideas to live. Currently working a lot in the JavaScript / React / NodeJS / GraphQL stack.

Updated on November 23, 2020

Comments

  • Fabian
    Fabian over 3 years

    I want to parse a user-input (using scanf) in 2 separate arrays. g++ compiles without error, but I get a memory-access-error (core dumped). (in german: "Speicherzugriffsfehler (Speicherabzug geschrieben)")

    char *top[10];
    char *bottom[10];
    
    for(i = 0; i < 5; i++){
        printf("Karte %d: Obere Werte? ", i );
        scanf( "%s", top[i] );
        printf( "Karte %d: Untere Werte? ", i);
        scanf( "%s", bottom[i] );
    }
    

    What's the problem here? I tried using "stringcpy" with a temp-var ("stringcpy(top[i], temp)"), but it didn't work either.

    Any suggestions?

  • Fabian
    Fabian over 11 years
    but why does top[i] = "test"; work? can you give me a little bit of example-code plz :) ?
  • Olaf Dietsche
    Olaf Dietsche over 11 years
    @user1781595 See the modified answer.
  • Fabian
    Fabian over 11 years
    if I try this, i get: "Fehler: ungültige Umwandlung von »void*« in »char*« [-fpermissive]" (in line of "top[i] = malloc(....)" "invalid conversation of "void*" to "char*" "...
  • Daniel Fischer
    Daniel Fischer over 11 years
    @user1781595 Compile as C, that complaint indicates you're compiling as C++.