In C, you can determine the size of an array using the sizeof
operator. However, the method depends on whether the array is static (fixed size) or dynamic (allocated with malloc
).
- For static arrays:
Here,
sizeof(arr)
gives the total memory size of the array, andsizeof(arr[0])
gives the size of one element. - For dynamic arrays (allocated with
malloc
), you must manually track the size sincesizeof
only gives the size of the pointer.