[c] How to find the size of integer array

How to find the size of an integer array in C.

Any method available without traversing the whole array once, to find out the size of the array.

This question is related to c arrays

The answer is


If array is static allocated:

size_t size = sizeof(arr) / sizeof(int);

if array is dynamic allocated(heap):

int *arr = malloc(sizeof(int) * size);

where variable size is a dimension of the arr.


int len=sizeof(array)/sizeof(int);

Should work.


_msize(array) in Windows or malloc_usable_size(array) in Linux should work for the dynamic array

Both are located within malloc.h and both return a size_t