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
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting the use of the `sscanf` function in C for parsing a string into separate words. Participants are examining issues related to uninitialized pointers and memory allocation, seeking to achieve the desired output of splitting the string "My first book" into three separate variables.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • Some participants point out that the pointers `a`, `b`, and `c` are uninitialized, which can lead to undefined behavior when used with `sscanf`.
  • There is a suggestion that the string `str` is not being utilized correctly in the `sscanf` function, as it should be passed as the first argument.
  • One participant mentions experiencing a segmentation fault when attempting to use `sscanf` with uninitialized pointers.
  • Another participant proposes declaring `a`, `b`, and `c` as arrays of fixed size instead of pointers to avoid memory issues.
  • Questions are raised about the participant's comfort level with pointers and their reasoning for using pointer declarations in this context.

Areas of Agreement / Disagreement

Participants generally agree that the use of uninitialized pointers is problematic, but there is no consensus on the best approach to resolve the issue or on the specifics of the implementation.

Contextual Notes

Limitations include the lack of initialization for the pointer variables and the potential for segmentation faults due to improper memory access. The discussion does not resolve the best practices for handling string parsing in C.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in string manipulation and memory management.

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 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K