Sscanf gives an error, take this code:

char * args = malloc(100); strcpy(args, "Simple_str 123"); char * str; int num = 0; sscanf(args, "%s %d", str, &num); printf("%s %d", str, num); 

Conclusion: Simple_str 123

It seems to be the way it should be, ok, let's take this code:

 char * args = malloc(100); strcpy(args, "Simple_str 123"); char * str; int num = 0; sscanf(args, "%s %d", str, &num); // <--- ошибка здесь printf("%s %d", str, num); memset(args, 0, 100); strcpy(args, "Simple_str"); sscanf(args, "%s", str); // <--- иногда здесь printf("%s; %s\n", args, str); 

How it works?

I compile: gcc -std=c99 main.c

    1 answer 1

    Both options are grossly erroneous. str is a pointer pointing to nowhere. And you are trying to write something through it. So it turns out "Segmentation fault".

    • Thank you so much, you just saved my day! Replaced, earned: char * str; ---> char * str = malloc(100); - I am in despair
    • @ I am in despair: ... and not "I am in despair", but "I am in despair." - AnT pm
    • oops ¯\_(ツ)_/¯ - I'm in despair
    • @AnT Or maybe he is not where, but where is he going? ... - Harry