Find the size of an integer array received as a function argument in c

I would like to find the size of the integer array passed as an argument to the function. Here is my code

void getArraySize(int arr[]){
  int len = (sizeof(arr)/sizeof(arr[0])
  printf("Array Length:%d\n",len);
}

int main(){
 int array[] = {1,2,3,4,5};
 getArraySize(array);
 return 0;
}

I get the following warning

sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument]

Please help so that I can find the length of the integer array inside function getArraySize However, I can find the size arrayinside the function main. Since it is referred to as pointer, I cannot find lengthfrom c function.

I have an idea, although I could add this to a block try/catch( Cdoesn't have a try catch, Only jumpers , which is OS dependent) and loop until I get it segmentation fault.

Is there any other way that I could use to find the length integer arrayinsidefunction

+4
4

. , , .

, , , :

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

.

, sizeof , .

, .

+4

, .

, void getArraySize(int arr[]) void getArraySize(int* arr).

sizeof(arr) sizeof(int*), , , , .

sizeof(arr)/sizeof(arr[0]) arr.

arr - main.

+3

. arr, sizeof(arr) . , - .

sizeof , .

int arr[5]; //real array. NOT a pointer
sizeof(arr); // :)

, , , sizeof , .

void getArraySize(int arr[]){
sizeof(arr); // will give the pointer size
}

, . , C ?

, , , .

, . , sizeof int *. , -

sizeof on array function parameter will return size of 'int *' instead of 'int []'

, , -

getArraySize(array, 5); // 5 is the number of elements in array

-

void getArraySize(int arr[], int element){
// your stuff
}

-

void getArraySize(int arr[], int len){
  // your stuff
  printf("Array Length:%d\n",len);
}

int main(){
 int array[] = {1,2,3,4,5};
 int len = (sizeof(arr)/sizeof(arr[0]); // will return the size of array
 getArraySize(array, len);
 return 0;
}
+2

All Articles