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

In summary: If so, could you please step through what you did?If I write like this, I am getting seg faultThe 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.
  • #1
anonim
40
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
  • #2
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.
 
  • #3
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
 
  • #4
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
 
  • #5
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
 
  • #6
So, what about your pointers? What's going on with those?
 
  • #7
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];
 
  • #8
@anonim, did you get something to work?
 

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

1. What is the purpose of using sscanf in C?

sscanf is a function in the C programming language that is used to read and parse data from a string. It is commonly used for input validation and data conversion.

2. How do I troubleshoot issues with string and sscanf in C?

If you are experiencing issues with getting correct output from string and sscanf in C, there are a few steps you can take to troubleshoot the problem. First, make sure that your string is properly formatted and matches the format specified in the sscanf function. You can also try using the debugger to step through your code and identify any errors. Additionally, checking for any potential memory leaks or buffer overflows can also help in troubleshooting.

3. Can sscanf be used to read data from multiple strings?

Yes, sscanf can be used to read data from multiple strings. You can specify multiple strings as arguments in the sscanf function, separated by a comma. However, the format string should match the order of the strings provided.

4. How do I handle errors when using sscanf in C?

When using sscanf, it is important to handle any potential errors that may occur. This can be done by checking the return value of the sscanf function, which indicates the number of successful conversions. If the return value is less than the number of expected conversions, it means that there was an error in parsing the string. You can then use the errno variable to get more information about the specific error that occurred.

5. Can sscanf be used to convert strings to other data types?

Yes, sscanf can be used to convert strings to other data types, such as integers or floats. This is done by using the appropriate format specifier in the format string. For example, %d for integers and %f for floats. However, it is important to ensure that the string being converted is in the correct format, otherwise, the conversion may fail.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
977
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
848
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top