Reputation: 19
#include<stdio.h>
void display(int *q,int,int);
int main(){
int a[3][4]={
2,3,4,5,
5,7,6,8,
9,0,1,6
};
display(a,3,4);
return 0;
}
void display(int *q,int row,int col){
int i,j;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d",*(q+i*col+j));
}
printf("\n");
}
printf("\n");
}
why this code show warning in gcc that "passing argument 1 of 'display' from incompatible pointer type display(a,3,4)"?...runs successfully anyway but curious to know about error..if anyone could tell this i would be grateful..
Upvotes: 0
Views: 56
Reputation: 215350
The rule of "array decay" means that whenever you use an array name a
as part of an expression, it "decays" to a pointer to the first element.
For a 1D array, this is pretty straight forward. An array int a [10]
would decay into type int*
.
However, in case of two-dimensional arrays, the first element of the 2D array is a 1D array. In your case, the first element of int a[3][4]
has array type int [4]
.
The array decay rule gives you a pointer to such an array, an array pointer, of type int (*)[4]
. This type is not compatible with the type int*
that your function expects.
However, by sheer luck, it would appear the the array pointer and a plain int pointer have the same representation on your system, and they happen to hold the same address, so the code works. You shouldn't rely on this though, it is not well-defined behavior and there is no guarantee it will work.
You should fix your program in the following way:
#include <stdio.h>
void display (int row, int col, int arr[row][col]);
int main()
{
int a[3][4]=
{
{2,3,4,5},
{5,7,6,8},
{9,0,1,6},
};
display(3, 4, a);
return 0;
}
void display (int row, int col, int arr[row][col])
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
printf("\n");
}
Here the array type that is the function parameter will silently "get adjusted" by the compiler to a pointer to the first element, int(*)[4]
, which matches what's passed to the function from the caller.
Upvotes: 1
Reputation: 53036
Because they are incompatible pointer types, it just happens that int *
could point to an array of int
s and int[][]
is an array of int
arranged in contigous memory.
The important difference is, that while you can access a
with a two index notation, like a[i][j]
you can't do that with q
.
Upvotes: 1