如何以及为什么使用c中的sizeof(a)/sizeof(a [0])计算数组中元素的数量 [英] how and why sizeof(a)/sizeof(a[0]) in c is used to calculate the number of elements in an array

查看:92
本文介绍了如何以及为什么使用c中的sizeof(a)/sizeof(a [0])计算数组中元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,我不知道sizeof(a)和sizeof(a [0])的确切含义来计算数组中元素的数量.为什么在何处使用此功能?划分它们的目的是什么.

I am a beginner to programming and i don't know the exact meaning of sizeof(a) and sizeof(a[0]) to calculate the no of elements in an array. Why and where is this function used ? And what is the purpose of dividing them.

推荐答案

根据C标准(6.5.3.4 sizeof和alignof运算符)

According to the C Standard (6.5.3.4 The sizeof and alignof operators)

2 sizeof运算符产生其操作数的大小(以字节为单位)可以是表达式或类型的括号名称.大小是由操作数的类型确定.结果是一个整数.如果操作数的类型是可变长度数组类型,操作数被评估;否则,不评估操作数,并且结果是一个整数常量.

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

因此,如果您有一个数组,例如

So if you have an array as for example

int a[N];

其中N是一个整数值,然后是表达式

where N is some integer value then expression

sizeof( a )

产生数组占用的字节数.由于数组具有 N 个元素,然后每个元素依次占用 sizeof(int)个字节

yields the number of bytes occupied by the array. As the array has N elements and each element in turn occupies sizeof( int ) bytes then

sizeof( a ) == N * sizeof( int )

或有什么相同

sizeof( a ) == N * sizeof( a[0] )

因此,您可以通过以下方式计算N

As result you can calculate N the following way

N = sizeof( a ) / sizeof( a[0] )

如果您不知道数组的确切大小,例如因为数组的大小取决于初始化程序的数量,则很有用.

It is useful if you do not know the exact size of an array for example because its size depends on the number of initializers.

例如

int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );

考虑到有时初学者会犯错误.

Take into account that sometimes beginners make an error.

让我们假设您有一个将数组声明为参数的函数.

Let's assume that you have a function declaring an array as a parameter.

例如

void f( int a[10] );

该函数可以像

int a[10];

f( a );

初学者通常在函数主体中编写以下表达式

Beginners usually write in the body of the function the following expression

void f( int a[10] )
{
    size_t n = sizeof( a ) / sizeof( a[0] );
    //...
}

但是它是错误的代码.问题是像数组一样声明的参数被调整为指向数组元素类型的指针.因此,函数声明实际上看起来像

However it is a wrong code. The problem is that parameters declared like arrays are adjusted to pointers to the type of the array element. So the function declaration actually looks like

void f( int *a );

以及表达式中的函数内

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

参数 a 是指针.那等于

    size_t n = sizeof( int * ) / sizeof( int );

取决于所使用的系统指针,占用4或8个字节.因此,如果 sizeof(int)等于4,您将得到2或1.

Depending on the used system pointers occupy either 4 or 8 bytes. So you will get either 2 or 1 if sizeof( int ) is equal to 4.

您将不会获得用作参数的数组中的元素数.

You will not get the number of elements in the array that was used as the argument.

指针不保留有关它们指向单个对象还是某个数组的第一个对象的信息.

Pointers do not keep an information about whether they point to a single object or the first object of some array.

在这种情况下,应使用第二个参数声明该函数,该参数指定数组中的元素数.例如

In this case you should declare the function with second parameter that specifies the number of elements in the array. For example

void f( int *a, size_t n );

这篇关于如何以及为什么使用c中的sizeof(a)/sizeof(a [0])计算数组中元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆