Raj
Raj

Reputation: 83

Warnings with pointers or declaration in multidimentional arrays while passing to a function

#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]);
}
} 
}

This is my code. I'm new to pointers. I'm getting this error. How do I rectify it ?

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:

I think the output is not being generated due to the warning.

Upvotes: 0

Views: 65

Answers (2)

JimmyNJ
JimmyNJ

Reputation: 1194

You define the variable a as

char a[MAXL][MAXD]

where MAXL is 18 and MAXD is 50 but the function floyd(..) is expecting a to be a type like

char [][18]

I'm not sure if you'll run off the edge of your array or not but you should either change the declaration of floyd(..) to expect a

char[MAXL][MAXD]

argument in the first position or change the variable a in main to be

char[][MAXL]

Hope this helps.

Upvotes: 1

NTAuthority
NTAuthority

Reputation: 371

In the floyd function declaration you declared a 18 column matrix. Instead you're passing a 50 column matrix. You should either remove the columns size in the declaration or adjust it to MAXD

Upvotes: 0

Related Questions