2016-07-11 29 views
1

這是我正在開發的程序。數組指向C程序中的指針問題

void primMST(int graph[N][N]) 
{ 
    int parent[N] ; // Array to store constructed MST 
    int key[N]; // Key values used to pick minimum weight edge in cut 
    bool mstSet[N]; // To represent set of vertices not yet included in MST 
    int i, count, v; 

    // Initialize all keys as INFINITE 
    for (i = 0; i < N; i++) 
     key[i] = INT_MAX, mstSet[i] = false; 

    // Always include first 1st vertex in MST. 
    key[0] = 0;  // Make key 0 so that this vertex is picked as first vertex 
    parent[0] = -1; // First node is always root of MST 

    // The MST will have N vertices 
    for (count = 0; count < N-1; count++) 
    { 
     // Pick thd minimum key vertex from the set of vertices 
     // not yet included in MST 
     int u = minKey(key, mstSet); 

     // Add the picked vertex to the MST Set 
     mstSet[u] = true; 

     // Update key value and parent index of the adjacent vertices of 
     // the picked vertex. Consider only those vertices which are not yet 
     // included in MST 
     for (v = 0; v < N; v++) 

      // graph[u][v] is non zero only for adjacent vertices of m 
      // mstSet[v] is false for vertices not yet included in MST 
      // Update the key only if graph[u][v] is smaller than key[v] 
      if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) 
      parent[v] = u, key[v] = graph[u][v]; 
    } 

    // print the constructed MST 
    printMST(parent, N, graph); 
} 



int main() 
{ 
    int i, j; 
    int** graph = (int**) malloc(sizeof(int*)*N); 
    for(i=0;i<N;i++) 
     graph[i] = (int*) malloc(sizeof(int)*N); 
    FILE *fp; 
    fp = fopen("AdjacencyMatrix_of_Graph_G_N.txt","r"); 
    char c; 
    for(i=0;i<N;i++) { 
     for(j = 0; j < N; j++) { 
      fscanf(fp, "%c ", &c); 
      graph[i][j] = c-'0'; 
     } 
    } 
    for(i=0;i<N;i++) { 
     for(j=0;j<N;j++) { 
      printf("%d ",graph[i][j]); 
     } 
     printf("\n"); 
    } 
    primMST(graph); 
    fclose(fp); 
    return 0; 
} 

我收到以下警告。

C:\TURBOC3\BIN\PROJECT\MST.c:125:10: warning: passing argument 1 of 'primMST' from incompatible pointer type 
    primMST(graph); 
     ^
C:\TURBOC3\BIN\PROJECT\MST.c:59:6: note: expected 'int (*)[30]' but argument is of type 'int **' 
void primMST(int graph[N][N]) 
    ^


Compilation results... 
-------- 
- Errors: 0 
- Warnings: 2 
- Output Filename: C:\TURBOC3\BIN\PROJECT\MST.exe 
- Output Size: 132.0068359375 KiB 
- Compilation Time: 0.48s 
+0

一個修正是將'primMST(int graph [N] [N])'改爲'primMST(int **圖)' – kaylum

+0

數組'int graph [N] [N]'是一個N * N整數。然而你的數組是一個指針數組,每個指向一個N個整數數組。結構不一樣。編譯器正在抱怨。 @ kaylum的評論給出了一個解決方案。 –

+0

@RudyVelthuis哪一行確實包含一個指針數組? – Michi

回答

0

正如人們在這裏評論的那樣,int **與int [] []不一樣,這就是編譯器抱怨的原因。

我也可以看到你動態地分配一個N數組的大小,這有兩個缺點: 1. malloc是一個非常昂貴的操作,並稱它N次似乎是不必要的。 2.結果將不會是一段連續的記憶。

假設N是一個#define,我會建議在main()的堆棧上分配graph [N] [N],而不是動態分配它。

+0

'malloc是一個非常昂貴的操作,並稱它N次似乎是不必要的'我會這樣做[This](http://ideone.com/YkxSwF) – Michi

+0

是的,這也是一個好方法,但這一切都取決於如果我需要一個小數組,我最好將其分配給堆棧,如果我不知道這些大小或大小非常大,我將在堆上進行分配。 – monkeyStix