I've discovered a segfault that I'm having trouble parsing. Lest you think I haven't searched, I don't think the issue is the same as in this question. I have the following typedef'd structure:
typedef struct usage usage;
struct usage{
char name[9];
int loc;
usage *next;
};
I'm reading data from a file that consists of a number K followed by K pairs (S,D) where S= an 8-character string [this is a variable name] and d= an integer [a memory location].
Here's the code that's causing the error:
void addUse(int index,char *nm, int addr){
usage *temp;
strcpy(temp->name,nm); //segfault here.
temp->loc = addr;
temp->next= NULL;
/* more processing */
}
To make this clearer, I am calling this function from a block where I have
int dummyIndex = 1;
char s1[9];
int val1;
scanf(" %s %d, s1, &val1);
addUse(dummyIndex, s1, val1);
It seems like in the question I linked to the issue is that they do not allocation the char
on the heap. I am not sure what's happening here. Using identical calls to strcpy
on another struct
with a field char name[9]
works just fine.
What am I missing? What have I over looked?
Thanks in advance!