小编Bou*_*ist的帖子

如何在C中为struct分配内存

你好,我有以下结构:

typedef struct GRAPHE{
        int n;    /* Nombre de sommets */
        int **M;  /* Matrice d'Adjacence */
    } GRAPHE; 
Run Code Online (Sandbox Code Playgroud)

我需要分配内存,我尝试了这个功能,但似乎没有用

void reservation_en_memoire(int n, GRAPHE *g) {
    int i, j;
    g = (GRAPHE *) malloc(n * sizeof (struct GRAPHE));
    g->M = (int **) malloc(n * sizeof (int*));
    for (i = 0; i < n; i++)
        g->M[i][j] = (int) malloc(n * sizeof (int));

    g->n = n;

}
Run Code Online (Sandbox Code Playgroud)

而在主要我只是这样做:

int main(int argc, char** argv) {

    GRAPHE *g;
    reservation_en_memoire(3, g);
    printf("%d", g->n);
    //  printf("%d", …
Run Code Online (Sandbox Code Playgroud)

c graph dynamic-memory-allocation

1
推荐指数
1
解决办法
452
查看次数

标签 统计

c ×1

dynamic-memory-allocation ×1

graph ×1