I am trying to set up an adjacent list array in C:
mygraph->table = (Node *)malloc(MaxSize * sizeof(Node));
check(mygraph->table, error);
int i;
for(i = 1; i <= MaxSize; i++)
{
mygraph->table[i].name = NULL;
mygraph->table[i].outlist = NULL;
mygraph->table[i].outdegree = 0;
}
...
When I run this code, it works fine. But when I try to access an index in the table I get a segmentation fault:
mygraph->table[n].name = name;
I checked the n index, and it is correct. MaxSize is 10, but I get a segmentation fault even when n = 1.
EDIT:
typedef struct linkedlist { // linked list of ints (for use in Node)
int index;
struct linkedlist *next;
} List;
typedef struct { // a Node of a Graph
char *name;
List *outlist; // adjacency list
int outdegree; // length of outlist
//double pagerank_score; //not needed for this exercise
} Node;
typedef struct {
// your code goes here
int MaxSize; /*Maximum number of vertices that the graph can constain*/
Node *table; /*Adjacency lists' array*/
} Graph;
name?