Code is as follows:
/* set.h */
struct setElement{
char *element;
setElement *next;
};
typedef struct setElement *Set; //Set is now the equivalent of setElement*
Set a;
setInit(&a);
/* setInit function declaration @ setInit.c */
int setInit(Set *a){
(*a)->element = "asdf"; //results in a seg fault
}
Trying to malloc ‘a’ works, but if I try to access any member within the set ‘a’ doesn’t work. I understand I’m passing a reference of the set from the main() function to setInit, so I believe the pointer contained within setInit is addressing the memory allocated by ‘Set a’ in the main() function, so a malloc wouldn’t be required…
Iunno. Help is appreciated 🙂
The problem is that you have not allocated the
setElementyou are trying to assign to. In the main part of the code you are creating aSet, which is just a pointer to asetElement. This pointer is never set to point to anything sensible. I.e. you need something like