Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c

memory

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

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

like image 593
Mark Harrison Avatar asked Sep 01 '08 06:09

Mark Harrison


People also ask

How do you identify the size of an array?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

Can you use sizeof on an array in C?

Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers.

What is sizeof () in C?

The sizeof() function in C is a built-in function that is used to calculate the size (in bytes)that a data type occupies in ​the computer's memory. A computer's memory is a collection of byte-addressable chunks.

What does size of array return in C?

sizeof is pure compile time in C++ and C prior to C99. Starting with C99 there are variable length arrays: // returns n + 3 int f(int n) { char v[n + 3]; // not purely a compile time construct anymore return sizeof v; }


2 Answers

Executive summary:

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

Full answer:

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); 
like image 168
Mark Harrison Avatar answered Sep 19 '22 20:09

Mark Harrison


The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.

Test:

#include <stdio.h> #include <stdlib.h>  void printSizeOf(int intArray[]); void printLength(int intArray[]);  int main(int argc, char* argv[]) {     int array[] = { 0, 1, 2, 3, 4, 5, 6 };      printf("sizeof of array: %d\n", (int) sizeof(array));     printSizeOf(array);      printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));     printLength(array); }  void printSizeOf(int intArray[]) {     printf("sizeof of parameter: %d\n", (int) sizeof(intArray)); }  void printLength(int intArray[]) {     printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) )); } 

Output (in a 64-bit Linux OS):

sizeof of array: 28 sizeof of parameter: 8 Length of array: 7 Length of parameter: 2 

Output (in a 32-bit windows OS):

sizeof of array: 28 sizeof of parameter: 4 Length of array: 7 Length of parameter: 1 
like image 35
Elideb Avatar answered Sep 17 '22 20:09

Elideb