27

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.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
AGeek
  • 5,165
  • 16
  • 56
  • 72
  • 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. – kennytm May 05 '10 at 12:56
  • Usually arrays are created as static variable and you must have passed some length while creating it. – Jack May 05 '10 at 13:35
  • 1
    @Jack: Why would arrays "usually" be static??? – sbi May 05 '10 at 13:38
  • int len = strlen(array); ??? – Joe DF Jan 18 '13 at 00: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? – Christian Rau May 14 '13 at 12:05
  • Possible duplicates (there are alot of these) http://stackoverflow.com/questions/16101836/size-of-a-dynamically-allocated-integer-array?lq=1 http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language http://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array http://stackoverflow.com/questions/7545428/size-of-array-in-c – Casper Beyer Apr 22 '14 at 04:40
  • possible duplicate of [How do I determine the size of my array in C?](http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c) – Casper Beyer Apr 22 '14 at 04:47

4 Answers4

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.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • Perhaps mention and point to a question explaining array decay? Frequent topic – Casper Beyer Apr 22 '14 at 04:38
  • 1
    For the C++ novices arriving at this C question: [Everything you ever wanted to know about arrays in C++](http://stackoverflow.com/q/4810664/140719) in one FAQ. – sbi Jan 09 '16 at 23:37
  • Are the parentheses around sizeof necessary? – lolololol ol Dec 25 '17 at 00:02
  • @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. – sbi Jan 01 '18 at 07:05
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.

user333453
  • 51
  • 2
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

Noobay
  • 377
  • 4
  • 15
0
int len=sizeof(array)/sizeof(int);

Should work.

Paul
  • 2,729
  • 20
  • 26
  • 2
    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 Gierlik May 05 '10 at 13:02
  • 4
    @Grzegorz: Show us how it works for this array: `void f(int array[]) { site_t len = sizeof(array)/sizeof(array[0]); }` – sbi May 05 '10 at 13:03
  • @Paul R: Why not? Unless `array` is an incomplete type (one case); if it's an array of `int` this will work. – CB Bailey May 05 '10 at 13:04
  • 1
    @sbi: The poorly named `array` parameter is actually a pointer. – caf May 05 '10 at 13:16
  • @caf: Yes, and that is something beginners keep running into. (I suppose a question like this _is_ from a beginner.) – sbi May 05 '10 at 13:20
  • 1
    @Charles: see sbi's answer as to why this only works for certain cases. – Paul R May 05 '10 at 13:26
  • I didn't see sbi's first comment when I wrote mine. Yes, in C99 variable length arrays are a second case. Obviously if `array` isn't an array but is actually a pointer then it's not going to work but there was no indication that this might be the case in the answer. – CB Bailey May 05 '10 at 13:42