Find the Size of integer array received as an argument to a function in c [duplicate]

You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.

If you want to know the size of an array passed to the function, you need to work it out before decay and pass that information with the array, something like:

void function (size_t sz, int *arr) { ... }
:
{
    int x[20];
    function (sizeof(x)/sizeof(*x), x);
}

Leave a Comment