okay guys, plz help, this is so wierd. I am not an expert in C, in fact, not an expert in anything.
I defined a struct as the following:
typedef struct Fact
{
char predicate[MAX_TOKEN_SIZE];
struct Fact* valuePtr;
struct Fact* next;
}Fact;
and i have some code like this, reading from a text file:
while(fgets(line, sizeof(line), inputFilePtr))
{
charPtr = line;
i = 0;
//buildStructure is a function that returns a Fact
Fact lineFact = buildStructure(line);
if(totalFact==0)
{
factsPtr = &lineFact;
}
//start of block A
puts("oooooooo");
puts((*((*factsPtr).valuePtr)).predicate);
if(hasValue(*((*factsPtr).valuePtr))){puts("has value");}
if(hasNext(*((*factsPtr).valuePtr))){puts("has next");}
puts("oooooooo");
//end of block A
totalFact++;
}
//block B, this block of code is the exact same copy of block A
puts("oooooooo");
puts((*((*factsPtr).valuePtr)).predicate);
if(hasValue(*((*factsPtr).valuePtr))){puts("has value");}
if(hasNext(*((*factsPtr).valuePtr))){puts("has next");}
puts("oooooooo");
.....
int hasNext(Fact f)
{
if(f.next!=NULL)
{
return TRUE;
}
return FALSE;
}
int hasValue(Fact f)
{
if(f.valuePtr!=NULL)
{
return TRUE;
}
return FALSE;
}
Although block B is the exact same copy of block A, their output is different:
block A gives:
oooooooo
tom
oooooooo
and block B gives:
oooooooo
tom
has value
has next
oooooooo
I just dont get it, because I think i didn't change anything after block A, but block B's output is different. My input text file has only one line and it seems after the while loop, the pointers in my struct suddenly points to something, while in the the while loop, it points to nothing. please give me some suggestions, THANK YOU!!!!