I've played with Graphs before and I managed it alright with some help from StackOverflow but I never used a structure like the one below. I can't seem to understand what I'm doing wrong here...
#include "stdio.h"
#include "stdlib.h"
#define MaxV 100
#define MaxE 50
typedef struct edge {
int dest;
int cost;
struct edge *next;
} Edge, *Graph[MaxV];
Graph *initGraph() {
Graph *g = (Graph*)malloc(sizeof(Edge) * MaxV);
for(int i = 0; i < MaxV; i++)
(*g[i])->next = NULL;
return g;
}
int main(void) {
Graph *g = initGraph();
for(int i = 0; i < MaxV; i++) {
if((*g[i])->next == NULL) printf("[%02d] NULL\n", i);
}
return 0;
}
I get a segmentation fault on the first iteration of (*g[i])->next = NULL;
and I can't understand why. I've tried countless things but I can't seem to manage the Graph initialization with such structure. Also, is the way I'm declaring and returning a pointer to a Graph done the right way for this structure?
Am I complicating things with lots of pointers in the init function or what?
P.S: Please do not suggest different structure definitions, I cannot changing anything in the one above. That's the real issue. I know how to work with Graphs rolling my own structure, but I need to use the one above.