Troubleshooting String and sscanf in C: How to Get Correct Output | Example Code

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C code String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
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?
 
on Phys.org
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?

exampleString.png
 
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.
output.JPG
 
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];