简体   繁体   English

是 arr[i] = i; 合法的?

[英]Is arr[i] = i; legal?

I want to initialize a value of all array members to their index.我想将所有数组成员的值初始化为其索引。

int main()
{
    int i;
    int arr[10];

    for (i = 0; i <= 9; i++)
        arr[i] = i;
}

Should I consider the sequence point in this case?在这种情况下我应该考虑序列点吗?
Is arr[i] = i legal and portable? arr[i] = i合法且可移植吗?

You need to consider sequence points if you modify something more than once in one place, or if you both read and modify something in one place.如果您在一个地方多次修改某些内容,或者如果您在一个地方同时阅读和修改某些内容,则需要考虑序列点。

You're not doing any of that, so your code is fine.你没有做任何这些,所以你的代码很好。

There is no restriction its just assigning a value to the array index没有限制它只是为数组索引分配一个值

If you just want an array of 0123456789 its fine如果你只想要一个 0123456789 的数组,那很好

Although its only one line its a good practice to use {}虽然它只有一行,但使用 {}

for (i = 0; i <= 9; i++)
{
   arr[i] = i;
}

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

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