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

2018-12-31 00:35发布

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

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

标签: c arrays memory
20条回答
唯独是你
2楼-- · 2018-12-31 01:22

The best way is you save this information, for example, in a structure:

typedef struct {
     int *array;
     int elements;
} list_s;

Implement all necessary functions such as create, destroy, check equality, and everything else you need. It is easier to pass as a parameter.

查看更多
心情的温度
3楼-- · 2018-12-31 01:23

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]), the size of the zeroeth 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);
查看更多
听够珍惜
4楼-- · 2018-12-31 01:29

@Skizz: I am pretty sure I am right, although the best "source" I can give you at the moment is Wikipedia, from the article on sizeof:

Wikipedia is wrong, Skizz is right. sizeof(char) is 1, by definition.

I mean, just read the Wikipedia entry really closely to see that it's wrong. "multiples of char". sizeof(char) can never be anything other than "1". If it were, say, 2, it would mean that sizeof(char) was twice the size of char!

查看更多
还给你的自由
5楼-- · 2018-12-31 01:30
sizeof(array) / sizeof(array[0])
查看更多
梦寄多情
6楼-- · 2018-12-31 01:32

"you've introduced a subtle way of shooting yourself in the foot"

C 'native' arrays do not store their size. It is therefore recommended to save the length of the array in a separate variable/const, and pass it whenever you pass the array, that is:

#define MY_ARRAY_LENGTH   15
int myArray[MY_ARRAY_LENGTH];

You SHOULD always avoid native arrays (unless you can't, in which case, mind your foot). If you are writing C++, use the STL's 'vector' container. "Compared to arrays, they provide almost the same performance", and they are far more useful!

// vector is a template, the <int> means it is a vector of ints
vector<int> numbers;  

// push_back() puts a new value at the end (or back) of the vector
for (int i = 0; i < 10; i++)
    numbers.push_back(i);

// Determine the size of the array
cout << numbers.size();

See: http://www.cplusplus.com/reference/stl/vector/

查看更多
ら面具成の殇う
7楼-- · 2018-12-31 01:33

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
查看更多
登录 后发表回答