2017-05-03 114 views
-2

我剛開始創建結構,並有興趣實現它以創建一個鄰接矩陣以用於圖相關的算法實現。所以我在圖中創建了一個指向指針變量的指針,用它作爲2D矩陣的基地址。但是,當我嘗試將內存分配給陣列時,它向我顯示一個錯誤:使用指針指向結構中的指針創建2d數組

Conversion to non-scalar type requested

任何人都可以幫我嗎?我張貼整個代碼如下所示: -

struct graph{ 
    int v; 
    int e; 
    struct graph **admat; 
}; 

void main() 
{ 
    int x,i,y,z=1,n; 
    struct graph *G=(struct graph **)malloc(sizeof(struct graph)); 
    printf("\nenter number of vertices: "); 
    scanf("%d",&G->v); 
    printf("\nenter number of edges: "); 
    scanf("%d",&G->e); 
    G->admat=(struct graph **)malloc(G->v * sizeof(struct graph *)); 
    for(i=0;i<G->v;i++) 
    { 
     G[i]=(struct graph)malloc(G->v * sizeof(int));//here is the main error 
    } 
    for(x=0;x<i;x++) 
    { 
     for(y=0;y<i;y++) 
     { 
      G[x][y]=z++; 
     } 
    } 
    for(x=0;x<i;x++) 
    { 
     for(y=0;y<i;y++) 
     { 
      printf(" %d ",G[x][y]); 
     } 
     printf("\n"); 
    } 
} 
+1

從'malloc'中刪除投射。如果你想持有'int','struct graph ** admat;' - >'int ** admat;' – BLUEPIXY

+0

G是一個單獨的指針,但是你正在類型化雙指針。那是不對的。我認爲你很困惑,G應該是一個雙精度指針還是結構元素admat是一個雙精度指針。這需要澄清。 –

+0

指定G [x] [y] = z不正確。 G是一個結構。它需要一個結構元素來存儲z值。 –

回答

1

這段代碼的問題是:

struct graph *G=(struct graph **)malloc(sizeof(struct graph)); 
printf("\nenter number of vertices: "); 
scanf("%d",&G->v); 
printf("\nenter number of edges: "); 
scanf("%d",&G->e); 
G->admat=(struct graph **)malloc(G->v * sizeof(struct graph *)); 
for(i=0;i<G->v;i++) 
{ 
    G->admat[i]=(struct graph)malloc(G->v * sizeof(int));//here is the main error 
} 

您應將其更改爲:

struct graph *G = malloc(sizeof(struct graph)); 
if (G == null) 
    printf("Error allocating memory"); 

printf("\nenter number of vertices: "); 
scanf("%d",&G->v); 
printf("\nenter number of edges: "); 
scanf("%d",&G->e); 

G->admat=malloc(G->v * sizeof(struct graph *)); // I guess you mean G->admat=malloc(sizeof(struct graph *)); 
if (G->admat == null) 
    printf("Error allocating memory"); 
for(i = 0; i<G->v; i++) 
{ 
    G[i] = malloc(G->v * sizeof(int)); 
    if (G[i] == null) 
     printf("Error allocating memory"); 
} 

應該被刪除,因爲您試圖爲G分配int s,這是一個指向struct graph的雙指針。這沒有任何意義。

也讀this link爲什麼你不應該投的malloc的結果。

+1

'G [i] =(struct graph)malloc(G-> v * sizeof(int));' - > 'G-> admat [i] = malloc(G-> v * sizeof(struct graph));',但是OP可能需要保存'int'。 – BLUEPIXY

+0

@BLUEPIXY編輯,謝謝。 – Marievi

+0

'G [i] = malloc(G-> v * sizeof(int));'出現越界。 – BLUEPIXY

0

假設admat包含二維矩陣數據,這裏是 代碼。引入一個新變量z來存儲值z。

#include <stdio.h> 
#include <stdlib.h> 

struct graph{ 
    int v; 
    int e; 
    int z; 
    struct graph **admat; 
}; 

void main() 
{ 
    int x,i,y,z=1,n; 
    struct graph *G= malloc(sizeof(struct graph)); 
    printf("\nenter number of vertices: "); 
    scanf("%d",&G->v); 
    printf("\nenter number of edges: "); 
    scanf("%d",&G->e); 

    G->admat=malloc(G->v * sizeof(struct graph *)); 
    for(i=0;i<G->v;i++) 
    { 
     G->admat[i]=malloc(G->v * sizeof(struct graph));//here is the main error 
    } 
    for(x=0;x<i;x++) 
    { 
     for(y=0;y<i;y++) 
     { 
      G->admat[x][y].z=z++; 
     } 
    } 
    for(x=0;x<i;x++) 
    { 
     for(y=0;y<i;y++) 
     { 
      printf(" %d ",G->admat[x][y].z); 
     } 
     printf("\n"); 
    } 
}