I understand how 1D array pointer works, but I can't understand how 2D array pointer works.
I know that array1[0] == *(array1 + 0), but I have no idea how to use this kind of form in 2D array. And I found out if I use double pointer or '[]'(i don't know what this is), then it works.
How can I access multi-dimensional arrays by using pointer?
Is it ok to use double pointer or '[]'? and if it is, why??
And why *(2Darrayname + number) isn't working?
Is it because of decay something? (I don't know about decay stuff sry)
edit) i already checked these What is array to pointer decay? Pointers in C: when to use the ampersand and the asterisk? Accessing multi-dimensional arrays in C using pointer notation
int main()
{
int array1[5];
int array2[5][5];
f1(array1);
f2(array2);
}
int f1(int *p1)
{
for(int i = 0; i<5 ; i++)
{
printf("%d", *(p1+i)); //this is ok
}
}
int f2(int (*p2)[5])
{
for(int i = 0; i<5 ; i++)
{
printf("%d", *(p2+i)); //this one makes error, and gcc says it is 'int *'
printf("%d", *(p2+i)[1]); //and this one is ok for some reason
printf("%d", **(p2+i)); //this one too. why is this ok??
}
}