English 中文(简体)
在接到要求时,先入选和重新启用阵列到零。
原标题:C initializes and re-initializes arrays to zero at runtime when asked
#include <stdio.h>

int main(void) {
    for (int i = 0; i < 5; i++) {
        int arr[5] = {0};
        // arr gets zeroed at runtime, and on every loop iteration!
        printf("%d %d %d %d %d
", arr[0], arr[1], arr[2], arr[3], arr[4]);
        // overwrite arr with non-zero crap!
        arr[0] = 3;
        arr[1] = 5;
        arr[2] = 2;
        arr[3] = 4;
        arr[4] = 1;
    }
    return 0;
}

很明显,这项工作:

> gcc -Wall -Wextra -pedantic -std=c99 -o test test.c;./test
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

但:

  • What s going on under the hood?
  • Is this guaranteed to work for arrays of any size?
  • Have I found the most elegant way of zeroing an array on every iteration of a loop?
最佳回答
问题回答

您正在每座播种中建立一个<><>><>>>新><>>>>>>>阵容,因此没有重新开始。

See according to me whenever you are iterating over a loop, then whatever variables you are declaring inside the loop are local to that loop. So at the end of the loop they are getting destroyed, and when the loop is again starting then they are being created again. So, 1. answered above; 2. yes this should work for array of any size; 3. no...

The variable arr is created and initialized on every iteration of the loop because that s what you wrote. In your example, the code does something analogous to memset() to set the array to zero, but possibly more efficiently and inline. You could write a non-zero initializer too.

是的,它针对的是任何大小的阵容。 如果阵列越大, lo就将放慢。

这是实现这一效果的非常明智的方法。 汇编者将尽快这样做。 如果你能够写一份更快捷的职能要求,汇编者作者就已经宣誓就职。

是的,《标准》保证,你的阵列 el在每一处 lo上都是零的。 如果你只想将其一度 zero灭,使阵列静态,即:

static int arr[5] = {0}; 

在此情况下,你可以删除<代码>={0},因为静态数据必须初步化,因为如果没有初始器,所有要素都被分配到0(或UNL为点人,或为浮动点值分配0.0),则重新计算。

请注意,如果您使用less initializers 而不是阵列,则其余部分被分配为零。 So

int arr[5] = {42, 0xdead};

is equivalent to

int arr[5] = {42, 0xdead, 0, 0, 0};

采用单一0办法只是这一规则的一个特殊案例。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签