By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Segmentation Faults using 'sscanf' and char pointers

P: 2

I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is with my assignment. I hope you guys can point me in the right direction.

I have a program which has an array of linked lists, with specific struct data elements inside each node of these lists. The linked lists are created dynamically and data (interger numbers) is read in from a file into these linked lists. Then the linked lists are manipulated in different ways.

The format of a line from the file is something like:

1 4 5 3 6 2 23 0
The spaces ARE included.

Now, the program compiles just fine, but I receive a segmentation fault at THIS line:

sscanf(subString, "%d", &numTerms);

...and all other sscanf function calls with subString. My debugger throws the error at this point. The purpose of this function call is to write to the subString character pointer a digit in the line of digits. Later on, the program is designed to skip over the space by placing a EOF character in it's place and beginning the string of digits/spaces right after the newly placed EOF, and repeat process. This allows 2+ digit numbers to be captured into the char array properly.

Here is a portion of my program so that it helps make sense of the variables:
struct poly
int coefficient;
int exponent;
struct poly *next;
//initialized variables
char stringTemp[ARYALLOC];
struct poly *arrayList[20];
//function takes line of scanned in file, and parses the "string" of digits
//and spaces
parseLine(stringTemp, arrayList);
void parseLine(char stringTemp[ARYALLOC], struct poly *arrayList[20])
int cmd;
char *subString;

subString = strtok(stringTemp, " ");

sscanf(subString, "%d", &cmd);

if(cmd == 1)
createPoly(subString, arrayList);
void createPoly(char *subString, struct poly *arrayList[20])
int numTerms, i, emptyIndex = 0;
struct poly *temp;

while(arrayList[emptyIndex] != NULL)

subString = strtok(NULL, "\0");
//segmentation fault occurs
sscanf(subString, "%d", &numTerms);
I've read that these errors can occur due to improper pointer referencing, but I thought that with the sscanf function that you could set a character pointer in there. I'm not really sure why this is occurring.

If anyone could help out in any way, I'd appreciate it greatly. Pointers confuse the heck out of me sometimes. Thanks so much!
Feb 19 '07 #1
Share this Question
Share on Google+
1 Reply

P: 2

I was looking up the sscanf function and determined that there was a much easier way to accomplish what I needed done. So, I no longer run into segmentation faults.

I determined though that the errors were being caused by an invalid derefrenced pointer to subString. Unfortunately, with the set up I had, I needed it set like that so I was kinda stuck. I'll defiinitely be back for more errors in the future, you guys seem really smart about a lot of things!
Feb 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.