【问题标题】:Warnings with pointers or declaration in multidimentional arrays while passing to a function传递给函数时在多维数组中带有指针或声明的警告
【发布时间】:2016-03-04 20:43:07
【问题描述】:
#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]);
}
} 
}

这是我的代码。我是指针的新手。我收到这个错误。我该如何纠正它?

在“main”函数中:警告:从“floyd”传递参数 1 不兼容的指针类型 [默认启用] 弗洛伊德(a,n,m); ^ 注意:预期为 'char ()[18]' 但参数的类型为 'char ()[50]' void floyd(char a[][18],int n, int m); /函数 定义/ ^

我当前的输出是

              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  

我们可以找到最短距离的最终矩阵:

我认为由于警告而没有生成输出。

【问题讨论】:

  • void floyd(char (*a)[MAXD],int n, int m) 怎么样?
  • 一个是用 18 作为第二维,另一个是 50。嗯,18 不等于 50,对吧?所以选择一个,并在两个地方都使用它。错误消息似乎很清楚问题所在。

标签: c arrays pointers multidimensional-array function-pointers


【解决方案1】:

您将变量 a 定义为

char a[MAXL][MAXD]

其中 MAXL 为 18,MAXD 为 50,但函数 floyd(..) 期望 a 为类似

char [][18]

我不确定你是否会跑出数组的边缘,但你应该更改 floyd(..) 的声明以期待一个

char[MAXL][MAXD]

第一个位置的参数或将main中的变量a更改为

char[][MAXL]

希望这会有所帮助。

【讨论】:

  • 这不起作用,声明需要数组的确切大小。它给出了错误:在函数'main'中:2d.c:14:10:错误:'a'char a[][MAXL]中缺少数组大小; ^
  • 尝试将 floyd(..) 声明和定义更改为 ... void floyd(char a[MAXL][MAXD],int n, int m);
【解决方案2】:

floyd 函数声明中,您声明了一个 18 列矩阵。相反,您传递的是 50 列矩阵。您应该删除声明中的列大小或将其调整为 MAXD

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多