在C中初始化大型二维数组 [英] Initializing a large two dimensional array in C
问题描述
我想初始化以下所有元素:
I want to initialize all elements of:
char buffer[1000][1000];
到' '
.
我尝试过
char buffer[1000][1000] = { ' ' };
和
char buffer[1000][1000] = { { ' ' } };
但是这两种方法似乎只能初始化第一个项.
but both ways only seem to initialize only the first term.
我愿意考虑完成任务的替代方法,但我宁愿避免在main中甚至在单独的初始化方法中编写这种初始化代码.
I am willing to consider alternative approaches that get the job done, but I would prefer to avoid writing in this kind of initialization code in main, or even a separate initialization method.
推荐答案
memset(buffer, ' ', sizeof buffer);
如果阵列具有自动存储期限,请考虑堆分配以避免堆栈溢出.
If the array has automatic storage duration, consider heap-allocation to avoid overflowing the stack.
如果数组具有静态存储持续时间,则仍然需要在运行时初始化数组,因为无法在不为每个元素提供单独的初始化值的情况下用非零值初始化元素.
If the array has static storage duration, you'll still need to initialize the array at runtime as there's no way to initialize elements with a non-zero value without providing a separate initialization value for each of them.
这篇关于在C中初始化大型二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!