0

PrintArray function

I have a problem determining how many elements in the array inside the function it works well outside the function why is that?? https://code.sololearn.com/cntt8cGTdFfk/?ref=app

24th Jan 2019, 5:21 AM
Mostafa Ehab
Mostafa Ehab - avatar
2 Answers
+ 4
When an array is passed to a function, it decays to a pointer and hence loses the size property. The sizeof() function will only show the size of the pointer and not the size of the entire array. The way around this is to provide the size information to the function as a parameter, before the decaying happens. void printArray(int* arr, int size); which can then be called in main as printArray(arr, sizeof(arr)/sizeof(int));
24th Jan 2019, 5:48 AM
Hatsy Rei
Hatsy Rei - avatar
0
I thought so.. thanks everyone 😃
24th Jan 2019, 9:28 AM
Mostafa Ehab
Mostafa Ehab - avatar