2016-03-04 64 views
0
#include <stdio.h> 

#define MAXB 32 
#define MAXL 18 
#define MAXD 50 

void floyd(char a[][18],int n, int m);/*Function definition*/ 

int main() 
{ 
int i = 0,temp,n,m,j=0,k=0,col; 
int numlines = 0,numcolumn=0; 
char buf[MAXB] = {0},c,check; 
char a[MAXL][MAXD]; 

FILE *fp = fopen("num.txt", "r"); 

if (fp == 0) 
{ 
    fprintf(stderr, "failed to open inputs/control.txt\n"); 
    return 1; 
} 

while (fscanf(fp, "%hhd", &a[i][k]) > 0) { 

    //check to see if the next character is a comma 
fscanf(fp, "%c", &check); 

//if it is a comma, go to the next char, else go to the next line 
if (check == ',') 
{k++; 
col++;} 
else 
{i++; 
k=0;} 

} 
numlines = i; 
m=((col/numlines)+1); 
printf("\n\t\t\t\t\tInput Matrix\n\n"); 
for (i = 0; i < numlines; i++){ 
printf("\t\t\t\t"); 
    for (j = 0; j <(col/numlines)+1; j++){ 
    if(a[i][j]==-12) 
printf("inf\t");/*Printing inf as infinity in the input matrix*/ 
else 

     printf (" %hhd\t",a[i][j]);} 
printf("\n"); 
} 

floyd(a,n,m); 

return (0); 
} 

void floyd(char a[][18],int n, int m)/*Function definition*/ 
{ 
int k,i,j; 
for(k=0;k<n;k++)/*n is the no.of vertices of the graph and k represents table no.*/ 
{ 
for(i=0;i<n;i++)/*i represents row no. within a table*/ 
{ 
for(j=0;j<m;j++)/*j represents column no. within a row*/ 
{ 
if(a[i][j]>(a[i][k]+a[k][j]))/*Minimum is to be selected*/ 
/*a[i][j] denotes distance between ith vertex and jth vertex*/ 
a[i][j]=(a[i][k]+a[k][j]); 
} 
} 
} 
printf("\n The final matrix where we can find the shortest distance:\n"); 
for(i=0;i<n;i++) 
{ 
printf("\ninside\n\n"); 
for(j=0;j<m;j++){ 
printf("%hhd",a[i][j]); 
} 
} 
} 

這是我的代碼。我是新手指針。我收到這個錯誤。我該如何糾正它?在傳遞函數時使用多指針數組中的指針或聲明的警告

In function ‘main’: warning: passing argument 1 of ‘floyd’ from incompatible pointer type [enabled by default] floyd(a,n,m); ^note: expected ‘char ()[18]’ but argument is of type ‘char ()[50]’ void floyd(char a[][18],int n, int m);/Function definition/ ^

My current output is

   Input Matrix 

      0 6 4 2 2 3 
      2 4 5 3 4 5 
      2 4 6 7 6 1 
      1 2 3 4 6 7 
      1 2 3 4 inf 4 

The final matrix where we can find the shortest distance:

我認爲由於警告而沒有生成輸出。

+1

'void floyd(char(* a)[MAXD],int n,int m)'而不是? –

+1

其中一個使用第二維18,另一個50.嗯,18不等於50,對吧?所以選一個,並在兩個地方使用它。錯誤消息似乎很清楚這個問題。 –

回答

0

floyd函數聲明中聲明瞭18列矩陣。相反,你傳遞了一個50列矩陣。您應該刪除的列大小在聲明或調整它MAXD

1

你定義一個變量作爲

char a[MAXL][MAXD] 

其中MAXL是18和MAXD是50但功能弗洛伊德(..)期待一個是一類像

char [][18] 

我不知道你是否會流失你的陣列與否的邊緣,但你應該要麼改變弗洛伊德的聲明(..),以期望

char[MAXL][MAXD] 

參數在所述第一位置或改變變量a在主是

char[][MAXL] 

希望這有助於。

+0

這不起作用聲明需要數組的確切大小。它給出的錯誤: 在函數'main'中: 2d.c:14:10:錯誤:數組大小在'a'中丟失 char a [] [MAXL]; ^ – Raj

+0

嘗試將floyd(..)聲明和定義更改爲... void floyd(char a [MAXL] [MAXD],int n,int m); – JimmyNJ