c - 传递给函数时,多维数组中的指针或声明出现警告

标签 c arrays pointers multidimensional-array function-pointers

#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:

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

最佳答案

您将变量 a 定义为

char a[MAXL][MAXD]

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

char [][18]

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

char[MAXL][MAXD]

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

char[][MAXL]

希望这有帮助。

关于c - 传递给函数时,多维数组中的指针或声明出现警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805672/

相关文章:

c - 如何将整数数字数组转换为整数值?

c - const struct pointer 是否保证内存损坏/崩溃问题的安全性?

c++ - 嵌套类指针的特殊行为?

c - 当用 1 个元素声明数组时如何动态分配内存

c - 以相同的优先级移位/归约

c - X86 汇编 - 处理 IDIV 指令

c++ - 从 ".exe"+ 偏移量读取内存?

c - 时间(空);与时间(&something);

c++ - 从 C++ 文件中读取后,如何将常规字符串数组转换为 const 字符串数组?

arrays - 如何从 Array{Array{Int64,2},1} 转换为 Array{Int64,2}