gpt4 book ai didi

c++ - 结构中的数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:26 27 4
gpt4 key购买 nike

我想创建一个结构,其中包含固定大小的数组:

struct smt{
int array1[3];
int array2[10];
int bananas;
};

这样我就可以在我的主代码中使用它。但是,当我尝试填充数组时,我总是会收到错误消息:

int main(){
smt name;
name.array1 = {1,2,3};

return 0;
}

错误在 name.array1 = {...};行:

error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'

如有任何帮助,我们将不胜感激。我试图找到类似的问题,但到目前为止还没有找到任何有用的东西。

最佳答案

如果它不在初始化中,你就不能这样做。你应该这样做:

name.array1[0] = 1;
name.array1[1] = 2;
name.array1[2] = 3;

看到这个有用 answer :

It's not just arrays, you cannot provide an initializer for anything at any point other than in a definition. People sometimes refer to the second statement of something like int i; i = 0; as "initializing i". In fact it's assigning to i, which previously holds an indeterminate value because it wasn't initialized. It's very rarely confusing to call this "initializing", but as far as the language is concerned there's no initializer there.

关于c++ - 结构中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com