简体   繁体   English

如何在sizeof运算符的帮助下初始化数组?

[英]How array is initialized with the the help of sizeof operator?

#include <stdio.h>
#include <stdarg.h>

int main()
{
    int x,y;

    y = sizeof(int[]){0,1,2,3,4} ;
    x = y / sizeof(1);

    printf("Number of arguments: %d", x);

    return 0;
}

This code gives the no. 此代码给出了编号。 of variables present in the array. 数组中存在的变量数。 (See variable y ) How this array is initialized? (请参见变量y )如何初始化此数组? I think the array is initialized this way: int a[]={variables} . 我认为数组是这样初始化的: int a[]={variables} If there are more ways to initialize the array please mention. 如果还有更多初始化数组的方法,请提及。

The code uses a compound literal to have an "inline" (anonymous) array. 该代码使用复合文字来具有“内联”(匿名)数组。

Since the array is only used with sizeof , no actual runtime array is constructed or initialized, this is all done at compile-time by the compiler simply inspecting the code and figuring out its type. 由于该数组仅与sizeof ,因此不会构造或初始化实际的运行时数组,因此,所有这些工作都由编译器在编译时完成,只需检查代码并确定其类型即可。

A quick godbolting gives us: 快速推销给我们:

push    rbp
mov     rbp, rsp
mov     DWORD PTR [rbp-20], edi
mov     QWORD PTR [rbp-32], rsi
mov     DWORD PTR [rbp-4], 20    # Load 'y' with 20 (size of 5 ints)
mov     eax, DWORD PTR [rbp-4]
cdqe
shr     rax, 2                   # Divide by four, giving 5.
mov     DWORD PTR [rbp-8], eax
mov     eax, DWORD PTR [rbp-8]
pop     rbp
ret

By the way: you're being inconsistent in your use of parentheses; 顺便说一句:您在使用括号时前后不一致; they're not needed with sizeof unless the argument is a type name, so the second use could be just 除非参数是类型名称,否则sizeof不需要使用它们,因此第二种用法可能只是

x = y / sizeof 1;

These two are commonly combined, but that's not possible here since your array is anonymous, of course. 这两个通常是结合在一起的,但是这里不可能,因为您的数组是匿名的。

(int[]){0,1,2,3,4 is a compound literal (introduced in C99) constructing an array. (int[]){0,1,2,3,4是构造数组的复合文字 (在C99中引入)。 This simply construct an array on the fly. 这只是在运行中简单地构造一个数组。

n1570-§6.5.2.5/p3: n1570-§6.5.2.5/ p3:

A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. 由括号类型名称和括号括起来的初始化程序列表组成的后缀表达式是复合文字。 It provides an unnamed object whose value is given by the initializer list. 它提供了一个未命名的对象,其值由初始化程序列表提供。 99) 99)

sizeof (int[]){0,1,2,3,4} gives the size of array (int[]){0,1,2,3,4} in bytes. sizeof (int[]){0,1,2,3,4}给出数组(int[]){0,1,2,3,4}的大小,以字节为单位。 sizeof (1) gives the size of an int and therefore y/ sizeof (1) gives the number of elements in the array. sizeof (1)给出int的大小,因此y/ sizeof (1)给出数组中元素的数量。

In this statement 在此声明中

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

there are two things. 有两件事。 The first one is there is used compound literal int[]){0,1,2,3,4} of the type int[5] . 第一个是使用类型为int[5]复合文字int[]){0,1,2,3,4} It is an unnamed integer array. 它是一个未命名的整数数组。 There is used the compound literal that to get the number of elements from the number of initializers. 使用了用于从初始化程序数量中获取元素数量的复合文字。 Take into account that neither array is created because the sizeof operator does not evaluate the expression. 考虑到两个数组都不创建,因为sizeof运算符不计算表达式。 It just determines its type. 它只是确定其类型。

In fact this statement is equivalent to 实际上,这句话等同于

y = sizeof( int[5] );

Using the compound literal just allows the compiler itself to calculate the number of elements in the array instead of doing this by you yourself. 使用复合文字只允许编译器本身计算数组中元素的数量,而不是您自己执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM