While I pass B[2][3] 2D Array in a function pointer decay is expected.
However that seems to be happening selective only in what I pass as a parameter syntactically.
Here is my code:
#include<stdio.h>
void printArray(int B[][3])
{
printf("sizeof(B) is : %un", sizeof(B)); //When arrays are passed to functions they decay into pointers.
printf("sizeof(B[0]) is : %un", sizeof(B[0])); //But here I get an array-pointer.Why there is no array to pointer decay here?See results Below.
}
int main(int argc, char* argv[])
{
int B[2][3] = { {2,3,6},{4,5,8} };
printf("sizeof(B) is : %un", sizeof(B));
printf("sizeof(B[0]) is : %un", sizeof(B[0]));
printArray(B);
return 0;
}
Here are my results:
sizeof(B) is : 24
sizeof(B[0]) is : 12
sizeof(B) is : 4
sizeof(B[0]) is : 12
C:UsersStrakizzzDesktopCMy Codeschool ProjectsPointers in C?C++DebugFunctionsPointersScopeMayhem.exe (process 20660) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
I was expecting while printing sizeof(B[0]) from my called function to get 4 bytes as well like when i calculated the sizeof(B) in the same function:
void printArray().
.Why do I get these results, what is happening under the hood?
New contributor
Sotiris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.