Ich versuche, eine benachbarte Liste Array in C einzurichten:Kann nicht Index der C-Array Zeiger zugreifen
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;
}
...
Wenn ich diesen Code ausführen, es funktioniert gut. Aber wenn ich versuche, einen Index in der Tabelle zuzugreifen, erhalte ich einen Segmentation Fault:
mygraph->table[n].name = name;
Ich überprüfte den n
Index, und es ist richtig. MaxSize
ist 10, aber ich einen Segmentierungsfehler auch erhalten, wenn 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;
Haben Sie Speicher für 'name' reserviert? – t0mm13b