## How do I determine the size of my array in C?

### Question

How do I determine the size of my array in C?

That is, the number of elements the array can hold?

2013/02/28
1
1054
2/28/2013 2:16:13 PM

Executive summary:

``````int a[17];
size_t n = sizeof(a)/sizeof(a[0]);
``````

To determine the size of your array in bytes, you can use the `sizeof` operator:

``````int a[17];
size_t n = sizeof(a);
``````

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

``````int a[17];
size_t n = sizeof(a) / sizeof(int);
``````

and get the proper answer (68 / 4 = 17), but if the type of `a` changed you would have a nasty bug if you forgot to change the `sizeof(int)` as well.

So the preferred divisor is `sizeof(a[0])` or the equivalent `sizeof(*a)`, the size of the first element of the array.

``````int a[17];
size_t n = sizeof(a) / sizeof(a[0]);
``````

Another advantage is that you can now easily parameterize the array name in a macro and get:

``````#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
``````
2020/04/27
1296
4/27/2020 7:55:16 PM

It is worth noting that `sizeof` doesn't help when dealing with an array value that has decayed to a pointer: even though it points to the start of an array, to the compiler it is the same as a pointer to a single element of that array. A pointer does not "remember" anything else about the array that was used to initialize it.

``````int a[10];
int* p = a;

assert(sizeof(a) / sizeof(a[0]) == 10);
assert(sizeof(p) == sizeof(int*));
assert(sizeof(*p) == sizeof(int));
``````
2014/10/06

The sizeof "trick" is the best way I know, with one small but (to me, this being a major pet peeve) important change in the use of parenthesis.

As the Wikipedia entry makes clear, C's `sizeof` is not a function; it's an operator. Thus, it does not require parenthesis around its argument, unless the argument is a type name. This is easy to remember, since it makes the argument look like a cast expression, which also uses parenthesis.

So: If you have the following:

``````int myArray[10];
``````

You can find the number of elements with code like this:

``````size_t n = sizeof myArray / sizeof *myArray;
``````

That, to me, reads a lot easier than the alternative with parenthesis. I also favor use of the asterisk in the right-hand part of the division, since it's more concise than indexing.

Of course, this is all compile-time too, so there's no need to worry about the division affecting the performance of the program. So use this form wherever you can.

It is always best to use sizeof on an actual object when you have one, rather than on a type, since then you don't need to worry about making an error and stating the wrong type.

For instance, say you have a function that outputs some data as a stream of bytes, for instance across a network. Let's call the function `send()`, and make it take as arguments a pointer to the object to send, and the number of bytes in the object. So, the prototype becomes:

``````void send(const void *object, size_t size);
``````

And then you need to send an integer, so you code it up like this:

``````int foo = 4711;
send(&foo, sizeof (int));
``````

Now, you've introduced a subtle way of shooting yourself in the foot, by specifying the type of `foo` in two places. If one changes but the other doesn't, the code breaks. Thus, always do it like this:

``````send(&foo, sizeof foo);
``````

Now you're protected. Sure, you duplicate the name of the variable, but that has a high probability of breaking in a way the compiler can detect, if you change it.

2008/10/15

``````int size = (&arr)[1] - arr;
``````

Check out this link for explanation

2014/01/10

You can use sizeof operator but it will not work for functions because it will take the reference of pointer you can do the following to find the length of an array:

``````len = sizeof(arr)/sizeof(arr[0])
``````

Code originally found here: C program to find the number of elements in an array

2017/11/18

If you know the data type of the array, you can use something like:

``````int arr[] = {23, 12, 423, 43, 21, 43, 65, 76, 22};

int noofele = sizeof(arr)/sizeof(int);
``````

Or if you don't know the data type of array, you can use something like:

``````noofele = sizeof(arr)/sizeof(arr[0]);
``````

Note: This thing only works if the array is not defined at run time (like malloc) and the array is not passed in a function. In both cases, `arr` (array name) is a pointer.

2014/09/08