Reputation:
I'm new in programming and learning pointers in array in C. Have a look at the below programmes.
1st program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int **p)
{
*(*(p+1)+1)=2135;
return 0;
}
2nd program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int *p)
{
*((p+1)+1)=2135;
return 0;
}
3rd program
#include<stdio.h>
int fun();
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
printf("%d",*(*(num+1)+1));
*(*(num+1)+1)=0;
printf("%d",*(*(num+1)+1));
return 0;
}
int fun(int (*p)[3])
{
*(*(p+1)+1)=2135;
return 0;
}
**p
is used in the fun()
function which I think it should be correct and in that function I've written *(*(p+1)+1)
to change the first element of first array. But on compiling this program it's showing error: invalid type argument of unary '*' (have 'int')
. As far as I know num is a pointer to array and it is holding the address of num[1]
which is again holding the address of num[1][0]
.*((p+1)+1)=0
is changing the value of 2nd element of first array. Why it is changing the value of 2nd element of zeroth array not the value of first element of first array? and How? It should be *(*(p+1)+1)=0
.*(p)[3]
mean?I had searched about this but couldn't found the satisfactory result.
Upvotes: 0
Views: 495
Reputation: 141554
All of your programs are ill-formed. Your compiler must produce warning or error messages, and the output of any executable produced is meaningless.
They are ill-formed because int[3][3]
is not compatible with int **
, nor with int *
, nor with int *[3]
.
To pass int[3][3]
to a function, the function must accept int (*)[3]
and nothing else (well, except for void *
).
This is because arrays can be converted to a pointer to the first element of the array. (In C syntax, num
can be used to mean &num[0]
).
In C, there are only truly one-dimensional arrays; an array of type int[3][3]
is considered to be an array of 3 elements, each of which is an array of 3 ints.
So a pointer to the first element of num
is a pointer to an array of 3 ints, which is written as int (*p)[3]
. You could write:
int (*p)[3] = &num[0];
or the shorthand for the same thing:
int (*p)[3] = num;
NB. You continually write *(*(num+1)+1))
which is difficult to read. Instead of this, num[1][1]
seems much clearer.
In C, x[y]
is always exactly equivalent to *(x+y)
.
Upvotes: 1
Reputation: 206577
I think you are asking: What's the difference between
int fun(int *p)
and
int fun(int (*p)[3])
The first one expects a pointer to an int
. The second one expects a pointer to an array of 3 int
s.
You are able to call to both these functions using num
since you declared the function as
int fun();
If you declare the functions like they are defined, you will get compiler error/warning for the first version.
Here's an updated version of your code and the resulting compiler warning, using gcc and compiler flag -Wall
.
#include <stdio.h>
int fun(int *p);
int main()
{
int num[3][3]={21,325,524,52,0,6514,61,33,85};
fun(num);
return 0;
}
int fun(int *p)
{
*(p+0)=2135;
return 0;
}
test.c: In function ‘main’:
test.c:7:4: warning: missing braces around initializer [-Wmissing-braces]
test.c:7:4: warning: (near initialization for ‘num[0]’) [-Wmissing-braces]
test.c:8:4: warning: passing argument 1 of ‘fun’ from incompatible pointer type [enabled by default]
test.c:3:5: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
Upvotes: 0