gpt4 book ai didi

c++ - 在声明时将静态二维数组初始化为零

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:46 30 4
gpt4 key购买 nike

我正在使用最新的 SDK 开发 iOS 5.0+ 应用。

我必须以这种方式声明一个初始化私有(private)二维数组,因为我要在 C++ 函数上使用它:

@implementation MyClass

int myArray[NSFirstDim][NSSecondDim] = 0; <-- Error here

...

- (id)init
{
...
}

...
@end

但我无法以这种方式初始化为 0,因为我收到以下消息:

Array initializer must be an initializer list

如何将所有值初始化为零?

或者我可以使用二维动态数组...

最佳答案

使用

int myArray[NSFirstDim][NSSecondDim] = {0};

这是来自 C 的强大行为,您可以在其中从开始添加值 并将其余(其他)初始化为 0。

在此示例代码中,

int arr[4][3]={1,2,3};

for (int i=0; i<4; i++) {
for (int j=0; j<3; j++) {
NSLog(@"%d",arr[i][j]);
}
}

Output: 1,2,3,0,0,0,....

关于c++ - 在声明时将静态二维数组初始化为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16275140/

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