I can't figure out why my code causes a segfault when I try the following give these structs:
typedef struct Vertex {
int i;
int color;
} vertex;
typedef struct Edge {
vertex v1;
vertex v2;
} edge;
typedef struct Node {
vertex *v;
struct Node *next;
} node;
node *nodehead = NULL;
code causing the issue here:
nodehead = malloc(sizeof(node));
if (nodehead == NULL) return -1;
nodehead->v->i = 10;
nodehead->next = NULL;
Maybe I'm missing something, most people had issues because they were trying to use the -> operator on an object. It seems trying to assign the int to the nodehead->v->i field causes the issue.
i. Look again. The problem is thatvis undefined, so an undefined pointer,v, is being dereferenced. Noti. Your version wouldn't even compile, since you're attemptingv.iwherevis a pointer.->dereferencesv(which is a pointer) and noti(which is an integer). So the termnodehead->v->iis technically correct regarding the types (but the pointer variable membervis uninitialized, which leads to the crash)p->x, then the "arrow" dereferencesp, notx.