sizeof的阵列澄清 [英] sizeof array clarification

查看:105
本文介绍了sizeof的阵列澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习在C最后的明天,并有一个问题关于sizeof操作符。

I am studying for a final tomorrow in C, and have a question regarding the sizeof operator.

假设的大小 INT 32 位和一个指针 64 位。

Let's say the size of an int is 32 bits and a pointer is 64 bits.

如果有一个函数:

int
foo (int zap[])
{
    int a = sizeof(zap);
    return a;
}

由于 ZAP 是一个指针,将返回 8 ,因为这是多少个字节需要如何保存这个特殊的指针。然而,具有以下code:

Because zap is a pointer, foo would return 8, as that's how many bytes are needed to store this particular pointer. However, with the following code:

int zip[] = { 0, 1, 2, 3, 4, 5 };
int i = sizeof(zip);

I 6 *的sizeof(INT) = 6 * 4 = 24

为什么说的sizeof(ZIP)返回每个元素的大小元素的个数倍,而的sizeof(扎普)返回一个指针的大小?难道的ZAP 尺寸是不确定的,而拉链是不是?编译器的知道的是拉链 6 元素,但没有一个线索如何大 ZAP 的可能。

Why is it that sizeof(zip) returns the number of elements times the size of each element, whereas sizeof(zap) returns the size of a pointer? Is it that the size of zap is unspecified, and zip is not? The compiler knows that zip is 6 elements, but doesn't have a clue as to how large zap may be.

推荐答案

这是排序在C语法的不对称。在C它不可能到一个数组传递给一个函数,所以当你使用数组语法在函数声明的参数之一,而不是编译器读取它作为一个指针。

This is sort of an asymmetry in the C syntax. In C it's not possible to pass an array to a function, so when you use the array syntax in a function declaration for one of the parameters the compiler instead reads it as a pointer.

在大多数情况下C当你在一个前pression使用数组数组被隐式转换为一个指向它的第一个元素,那就是例如,当你调用一个函数究竟发生了什么。在下面的code:

In C in most cases when you use an array in an expression the array is implicitly converted to a pointer to its first element and that is exactly what happens for example when you call a function. In the following code:

int bar[] = {1,2,3,4};
foo(bar);

阵列被转换为指针的第一个元素,并且是函数接收什么

the array is converted to a pointer to the first element and that is what the function receives.

隐式的转换这个规则并不总是能够适用。当你发现例如的sizeof 操作符阵列上,甚至&安培; (地址)的运营工作原来阵列上(即的sizeof(*&安培;巴)== 4 * sizeof的(INT))。

This rule of implict conversion is not however always applied. As you discovered for example the sizeof operator works on the array, and even & (address-of) operator works on the original array (i.e. sizeof(*&bar) == 4*sizeof(int)).

C中的函数不能recevive数组作为参数,它只能接收一个指向第一个元素,或指向数组...或者你必须包装在一个结构数组。

A function in C cannot recevive an array as parameter, it can only receive a pointer to the first element, or a pointer to an array... or you must wrap the array in a structure.

即使你把括号之间的数在函数声明...

Even if you put a number between the brackets in the function declaration...

void foo(int x[4])
{
    ...
}

这数字完全是由编译器忽略......这声明编译器是完全等同于

that number is completely ignored by the compiler... that declaration for the compiler is totally equivalent to

void foo(int *x)
{
    ...
}

和例如甚至称它传递数组不同尺寸将不会触发任何错误...

and for example even calling it passing an array with a different size will not trigger any error...

int tooshort[] = {1,2,3};
foo(tooshort);  /* Legal, even if probably wrong */

(实际上是一个编译器的可以的给予警告,但code是完全合法的C和必须如果编译器遵循标准被接受)

(actually a compiler MAY give a warning, but the code is perfectly legal C and must be accepted if the compiler follows the standard)

如果你认为这条规则对数组的函数参数的时候很奇怪,然后我同意,但是这是C语言是如何定义的。

If you think that this rule about arrays when in function arguments is strange then I agree, but this is how the C language is defined.

这篇关于sizeof的阵列澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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