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;
}
- In the first program
**p
is used in thefun()
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 showingerror: invalid type argument of unary '*' (have 'int')
. As far as I know num is a pointer to array and it is holding the address ofnum[1]
which is again holding the address ofnum[1][0]
. - On compiling the second program compiler is showing no error. And
*((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
. - In the third program the compler is showing no error and it is showing the correct result. How?. What does
*(p)[3]
mean?
I had searched about this but couldn't found the satisfactory result.