473,411 Members | 2,285 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Segmentation Faults using 'sscanf' and char pointers

2
Hello!

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)
emptyIndex++;

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
1 4429
ziroi
2
Update:

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

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

Similar topics

15
by: AMT2K5 | last post by:
Hello folks, I seem to have recieved a segfault within my function but am unsure on how to resolve it. I understand that it means that somewhere something is trying to access memory that is not...
13
by: N.S. du Toit | last post by:
Just having a bit of trouble programming with C under FreeBSD 5.1 using the gcc compiler. I'm a bit new to C so my apologies if the answer to my question appear obvious :) Basically I've...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
22
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s",...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
14
by: cnixuser | last post by:
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
6
by: DrSchwartz | last post by:
Hi all, I'm completely stuck.. I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.