2017-02-22 16 views

ответ

2

Как это:

graph g; 
g.number_vertices = n; 
g.vertices = malloc(n * sizeof(vertex)); // allocate dynamic memory for `n` vertices 
             // and make g.vertices point to that memory 
// fill g.vertices[0], g.vertices[1], …, g.vertices[n-1] 

// … 

// deallocate the memory when you're done: 
free(g.vertices); 
0

Выделяют буфер достаточно большой, чтобы хранить вершины и хранить указатель на него в переменной vertices в структуры .

struct graph g; 
g.number_vertices = 10; // If you want to store 10 vertices 
g.vertices = (vertex*)malloc(g.number_vertices * sizeof(struct vertex)); 
g.vertices[0]... // To access the first. 
g.vertices[1]... // To access the second. 
g.vertices[2]... // To access the third, etc. 
+1

[Вы не должны указывать результат malloc] (http://stackoverflow.com/q/605845/3425536). – emlai

Смежные вопросы