Sizeof array through function in C [duplicate]

好久不见. 提交于 2019-11-28 14:39:20
int searchMe(int numbers[], int target)

is equivalent to

int searchMe(int *numbers, int target)

There is a special C rules for function parameters that says parameters of array type are adjusted to a pointer type.

It means in your program that sizeof numbers actually yields the size of the int * pointer type and not of the array.

To get the size you have to add a third parameter to your function and explicitly pass the size of the array when you call the function.

Because arrays are passed as reference. Meaning only pointer to array is passed. Sizeof inside the function would only print the size of pointer, not the size of array.

Besides just making sure you know, size of returns number of bytes, so if you have an integer array (where int is of size 4 for e.g. sizeof would give elements * 4 as output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!