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.
    – kennytm
    May 5, 2010 at 12:56
  • Usually arrays are created as static variable and you must have passed some length while creating it.
    – Jack
    May 5, 2010 at 13:35
  • 1
    @Jack: Why would arrays "usually" be static???
    – sbi
    May 5, 2010 at 13:38
  • int len = strlen(array); ???
    – Joe DF
    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? 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.

4
  • Perhaps mention and point to a question explaining array decay? Frequent topic Apr 22, 2014 at 4:38
  • 1
    For the C++ novices arriving at this C question: Everything you ever wanted to know about arrays in C++ in one FAQ.
    – sbi
    Jan 9, 2016 at 23:37
  • Are the parentheses around sizeof necessary? Dec 25, 2017 at 0: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 1, 2018 at 7: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.

1
  • Dont ever use size name for identifier it may cause problems.
    – HeavenHM
    Nov 19, 2018 at 1:04
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

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

Should work.

7
  • 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. May 5, 2010 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 5, 2010 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 5, 2010 at 13:04
  • 1
    @sbi: The poorly named array parameter is actually a pointer.
    – caf
    May 5, 2010 at 13:16
  • 1
    @Charles: see sbi's answer as to why this only works for certain cases.
    – Paul R
    May 5, 2010 at 13:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.