28

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.

7
  • 14
    How did you implement this array? In principle either you know the array size in O(1) (known size), O(N) (nil-terminated), or impossible. Commented May 5, 2010 at 12:56
  • Usually arrays are created as static variable and you must have passed some length while creating it. Commented May 5, 2010 at 13:35
  • 1
    @Jack: Why would arrays "usually" be static??? Commented May 5, 2010 at 13:38
  • int len = strlen(array); ??? Commented Jan 18, 2013 at 0:04
  • 2
    "Any method available without traversing the whole array once, to find out the size of the array." - I would rather like know how you would traverse the array without knowing its size beforehand? Commented May 14, 2013 at 12:05

4 Answers 4

84

If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.

If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.
Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps mention and point to a question explaining array decay? Frequent topic
For the C++ novices arriving at this C question: Everything you ever wanted to know about arrays in C++ in one FAQ.
Are the parentheses around sizeof necessary?
@lololololol In principle, you can use sizeof without the parentheses. Except in some circumstances. I never even tried to read the fineprint, and instead subscribed to the common practice to always employ them.
5

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.

1 Comment

Dont ever use size name for identifier it may cause problems.
3

_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

Comments

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

Should work.

7 Comments

It works, however site_t len = sizeof(array)/sizeof(array[0]); it's a bit better (i.e. it still works when datatype of array elements has been changed.
@Grzegorz: Show us how it works for this array: void f(int array[]) { site_t len = sizeof(array)/sizeof(array[0]); }
@Paul R: Why not? Unless array is an incomplete type (one case); if it's an array of int this will work.
@sbi: The poorly named array parameter is actually a pointer.
@Charles: see sbi's answer as to why this only works for certain cases.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.