I'm trying to change the 'lineNumber' field of this struct:
typedef struct occurrenceType Occurrence;
struct occurrenceType {
char* line;
int lineNumber;
int wordNumber;
Occurrence *next;
};
using this bit of code
Occurrence *occur;
occur=(Occurrence*)malloc(sizeof(Occurrence));
(*occur).lineNumber=34;
but when I print (*occur).lineNumber it comes to be zero. I've tried several different configurations with the struct and the occur pointer, but nothing seems to be working. Can anyone see what I'm doing wrong? Thanks!
EDIT: The full call looks like this:
inFile=fopen(argv[1],"r");
while(fgets(line,100,inFile)!=NULL) {
if(strstr(line,argv[2])!='\0') {
(*occur).line=line;
(*occur).lineNumber=34;
(*occur).next=(Occurrence*)malloc(sizeof(Occurrence));
occur=(*occur).next;
printf("%d",(*occur).lineNumber);
}
count++;
}
It reads a file line by line and searches for a key provided in the command line, then adds a struct to a linked list for each occurrence.
malloc. In C it's a bad idea, because in C that's just telling the compiler to shut up about failure to include necessary header.(*occur).lineNumberyou can just writeoccur->lineNumber. ;-)