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

  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C code String
AI Thread Summary
The discussion centers on troubleshooting the use of `sscanf` in C to parse a string into separate words. The original code fails because the pointer variables `a`, `b`, and `c` are uninitialized, leading to undefined behavior. To resolve the issue, these pointers should be replaced with character arrays that have sufficient size to hold the substrings. The corrected code uses `sscanf(str, "%s %s %s", a, b, c)` to properly read from the initialized string. This change prevents segmentation faults and allows the program to output the desired results.
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?
 
Physics news 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];
 

Similar threads

Replies
2
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
2K
Replies
5
Views
3K
Replies
2
Views
2K
Replies
3
Views
1K
Back
Top