Why does sscanf fail to parse strings into variables?

  • Topic: Comp Sci 
  • Thread starter anonim
  • Start date
  • Tags Tags
    C code String
7 replies · 2K views · last reply
anonim
Messages
39
Reaction score
2
Homework Statement
Breaking the string into words
Relevant Equations
-
C:
#include<stdio.h>
int main(){
    char str[]="My first book";
    char *a,*b,*c;
    sscanf("%s %s %s",a,b,c);
    printf("a=%s b=%s c=%s",a,b,c);

}

I want the output: a= My b=first and c=book. But it does not work, why?
 
anonim said:
Homework Statement:: Breaking the string into words
Relevant Equations:: -

C:
#include<stdio.h>
int main(){
    char str[]="My first book";
    char *a,*b,*c;
    sscanf("%s %s %s",a,b,c);
    printf("a=%s b=%s c=%s",a,b,c);

}

I want the output: a= My b=first and c=book. But it does not work, why?
When you say "it does not work," what do you mean by that?
Are you getting a compiler error? Run-time error?
Is there any output?

One problem that I see immediately is that your a, b, and c pointer variables are uninitialized -- whatever memory they point to isn't something that you have set up. You should never use a pointer that hasn't been initialized to a valid memory buffer.
 
When did you use str besides its declaration? I would take another look at your sscanf.

edit:

You're almost there. I made some minor changes and it worked for me. Are you comfortable with pointers by the way or why did you choose to declare them as pointers?

Screenshot of a Quincy 2005 console window with black background showing the output: a=My b=first c=book, followed by Press Enter to return to Quincy...
 
Mark44 said:
When you say "it does not work," what do you mean by that?
Are you getting a compiler error? Run-time error?
Is there any output?

One problem that I see immediately is that your a, b, and c pointer variables are uninitialized -- whatever memory they point to isn't something that you have set up. You should never use a pointer that hasn't been initialized to a valid memory buffer.
Screenshot of program output showing garbled text: a=UHAWAVAUATSH(L% b=(null) c=AWAVIAUATL%, with unreadable characters, likely a corrupted variable dump.
 
Joshy said:
When did you use str besides its declaration? I would take another look at your sscanf.

edit:

You're almost there. I made some minor changes and it worked for me. Are you comfortable with pointers by the way or why did you choose to declare them as pointers?

View attachment 262830
C:
#include<stdio.h>
int main(){
    char str[]="My first book";
    char *a,*b,*c;
    sscanf(str,"%s %s %s",a,b,c);
    printf("a=%s b=%s c=%s",a,b,c);

}

If I write like this, I am getting seg fault
 
So, what about your pointers? What's going on with those?
 
anonim said:
C:
#include<stdio.h>
int main(){
    char str[]="My first book";
    char *a,*b,*c;
    sscanf(str,"%s %s %s",a,b,c);
    printf("a=%s b=%s c=%s",a,b,c);

}

If I write like this, I am getting seg fault
The reason you're getting a seg fault is that sscanf() is attempting to store parts of the character string into memory that doesn't belong to the program.

Instead of declaring a, b, and c as you have done (as uninitialized pointers), try declaring them as arrays of some fixed size, with each one declared to be large enough to hold a substring of the original string.
C:
char a[20], b[20], c[20];