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!