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

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

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

Solutions

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);

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

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));
Tags: C / Arrays / Memory

Similar questions

Reallocating a 2d array if initialized like so: char (*A)[size] = malloc(sizeof(char[size][size]))
If I initialize size as 10, but eventually I need more memory, how can I reallocate memory for A without losing its contents? I tried something like this: I start running into problems when I need to reallocate. If I input 1,2,3,4 above I want: which I can successfully output if I initiallize sizeColumns as 5 above, but if it is 4 then I get: I don...
Determine the size of character array in memory
I'm a beginner programmer in C so excuse me if my language/style is not professional. Question: I'm trying to determine the size of a character array, so that I can later on know the size to allocate using malloc(). My code: The problem is I want to determine the size in memory of src1 and src2. i.e. size of src1 should be 11, and size of src2 shou...
C
Is there a way to determine the size of an array based on t
I wrote a function that calculates outer differences between an array and this array itself, and only saves those results that are positive (see code below) Then, in the function below, I call outer_pos. One issue is that the array xouter may contain tied values. Without tied values, the number of positive differences is (nsamp)(*nsamp-1)/2, but wi...
Determine size of character array in C
I am trying to write a program that will talk to a XBee Pro S2B over serial from a PIC16 and I have some odd memory stuff going on... I am a bit weak when it comes to pointers in C so I have been writing a test program using GCC to get it to work before I move it over to the PIC microcontroller. The biggest thing to note with the PIC is the memory ...
How do i determine the size of an array from a character read from a file?
I have to allocate a dynamic array and i know how many columns there will be on the array but i don't know how many rows, all i have is a number on a .txt file. I have tried the following code but i am not sure it will work: Since the ascii value of 0 is 48, i assumned that i needed to cast the character read from the file in order to be able to us...
C
what actually happens if we try to determine the size of an array after passing it to a function
In this code the answer is always 2. I am unable to understand why.Is it necessary to send the size always.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.