How do I determine the size of my array in C?
In C, arrays do not come with a built-in property or method that directly tells you their size (i.e., the number of elements). However, there is a common idiom that works reliably for statically allocated arrays. Below, we’ll cover how that idiom works, clarify some edge cases, and offer guidance on best practices.
1. Use the sizeof
Operator
If you have a statically allocated array (declared with a fixed size at compile time, like int arr[10]
), you can determine its length by dividing the total memory size of the array by the size of each element:
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; // Calculate the array size size_t arr_size = sizeof(arr) / sizeof(arr[0]); printf("The array has %zu elements.\n", arr_size); return 0; }
- Explanation:
sizeof(arr)
returns the total number of bytes occupied by the array.sizeof(arr[0])
returns the number of bytes per element.- Dividing the former by the latter gives the total element count.
2. Recognize the Scope of This Trick
- Function Parameters: If you pass
arr
as a function parameter likemyFunction(int arr[])
, the array “decays” into a pointer, losing information about its size. In that context,sizeof(arr)
would give you the size of a pointer, not the array. - Dynamically Allocated Memory (Heap): For arrays created with
malloc
,realloc
, orcalloc
, you only receive a pointer to the allocated memory. The compiler doesn’t track how much space that pointer references, so thesizeof
trick won’t work.
3. Handling Function Parameters
If you need the size inside a function, you can:
- Pass the array length as an additional parameter:
void printArray(int arr[], size_t length) { for (size_t i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); }
- Use sentinel values to indicate the end of an array (similar to how C-strings end with
'\0'
), but this only works for specific data patterns.
4. Dynamic Memory Scenarios
For heap-allocated arrays:
int *arr = malloc(sizeof(int) * 5); // ... // There's no built-in function to find out "5" from `arr` alone. // You must track the size yourself, e.g.: size_t length = 5;
You should store length
in a separate variable or structure to keep track of how many elements you allocated.
5. Best Practices & Interview Tips
- Be Clear About Scope: Always remember that
sizeof
only works to find the total array size if you’re dealing with a locally declared array (static or automatic storage) in the same scope. - Maintain Consistency: If you anticipate passing arrays around, maintain and pass the array size as a separate argument.
- Avoid Off-by-One Errors: Double-check your loops and array accesses—particularly in C, where you’re responsible for bounds checking.
Enhance Your Foundations in Systems Programming
If you’re aiming to strengthen your overall understanding of data structures, algorithms, and their implementation details (including low-level languages like C), consider exploring these resources from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews – Dive into in-depth coverage of arrays, pointers, memory management, and more—knowledge that translates well to any systems-level language like C.
- Grokking the Coding Interview: Patterns for Coding Questions – Learn how to quickly identify recurring coding patterns, a valuable skill in interviews and real-world problem solving.
You can also elevate your interview readiness by booking a Coding Mock Interview with experienced ex-FAANG engineers who can offer targeted feedback.
Key Takeaway: In C, use sizeof(arr) / sizeof(arr[0])
to determine the element count of a statically allocated array. For dynamically allocated arrays or within function parameters, you must explicitly track and pass the size.