Essentially, I have a structure for a linked list in my code,
struct node{
char* val;
struct node *next;
};
Then later I try to do some things to it...
...
addr = malloc(sizeof(struct node));
addr->val = malloc(72);
addr->val = "";
snprintf(addr->val, 1000, "%s", ident);
...
... This gives me a segmentation fault at the snprintf. Valgrind says the following
Process terminating with default action of signal 11 (SIGSEGV)
==10724== Bad permissions for mapped region at address 0x40566B
==10724== at 0x4EB0A32: vsnprintf (vsnprintf.c:112)
==10724== by 0x4E8F931: snprintf (snprintf.c:33)
==10724== by 0x4016CC: id (Analyzer.c:267)
...
I am fairly new to C as opposed to C++, but I thought that calling malloc on the char* should make it valid, especially since I can initialize and print it, so I don't understand why the snprintf wouldn't work. I also had my program print out the addresses of both variables, and the address valgrind complains about is indeed from addr->val.
I also tried using strcpy instead of snprintf but had the same result.
Thanks.