I am getting a bus error with the following line of code (and presumably the 3 similar lines following it):
new->f1 = u;
this is my struct:
Code:
struct node // node for linked list
{
int f1;
float f2;
char f3;
struct node *next; // PTR to next node
};
and this is the function where it is being filled. The function takes in a line out of a file which contains an integer, a float, then a character and it is trying to capture the individual tokens and put them into the struct node.
Code:
in main: new = tokenize(line);
struct node* tokenize(char line[])
{
int search = 0; // will search each line
int new2 = 0; // will track temp field
int tempINT[10];
int tempFLOAT[10];
char tempSTRING;
while (line[search] != ' ')
{
tempINT[new2] = line[search]-48; // -48 will convert to integer
new2++;
search++;
}
new2 = 0;
search++;
while (line[search] != ' ') // digits before '.'
{
tempFLOAT[new2] = line[search]-48;
new2++;
search++;
if (line[search] == '.') // if float has a decimal place
{
search++;
while (line[search] != ' ') // digits after '.'
{
tempFLOAT[new2] = line[search]-48;
new2++;
search++;
}
}
}
search++;
tempSTRING = line[search]; // next value is char
//////// important stuff:
int u = (tempINT[0]*10 + tempINT[1]);
fprintf(stderr, "Here in tokenize u is %d\n", u);
new->f1 = u; //ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// new->f1 = (tempINT[0]*10 + tempINT[1]);
fprintf(stderr, "further in tokenize\n");
new->f2 = (tempFLOAT[0] + tempFLOAT[1]/10.0);
new->f3 = tempSTRING;
new->next = NULL;
return new;
}
the printf before the problem statement works, but the one after it does not.
i thought maybe changing it to new.f1 = u; but this does not work either.
i also tried redefining the function to return *new, and this does not work either.
i am very puzzled as to why this will not assign correctly. thanks for taking a look