Jose Herrera
Jose Herrera

Reputation: 3

Problem with C language, I don't understand the error

I'm learning C but I don't understand why the error appears on line number 9 ---> scanf("%[^\n]s",&cadena); // i tried with "%s" but still doesnt work.

#include <stdio.h>

int main(void)
{
    char cadena[40];

    printf("Ingrese cadena: ");
    fflush(stdin);
    scanf("%[^\n]s",&cadena);
    printf("La cadena es %s \n", cadena);

    return (0);
}

Upvotes: 0

Views: 73

Answers (1)

pseudovella
pseudovella

Reputation: 238

Remove the ampersand on cadena in scanf

scanf("%[^\n]", cadena);

An array decays to a pointer so you were actually passing a pointer to a pointer in that case.

Also you can just write it like this

scanf("%s",cadena);

Depends on your end goal though.

Upvotes: 4

Related Questions