初始化int数组 [英] initializing an array of ints
本文介绍了初始化int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人有一种方法来初始化的 INT
秒的阵列(任何多字节类型是罚款真的),到非零和非-1价值根本?我的意思是,有没有办法做到这一点的一个内胆,无需逐个做的每一个元素:
Does anyone have a way to initialize an array of int
s (any multi-byte type is fine really), to a non-zero and non -1 value simply? By which I mean, is there a way to do this in a one liner, without having to do each element individually:
int arr[30] = {1, 1, 1, 1, ...}; // that works, but takes too long to type
int arr[30] = {1}; // nope, that gives 1, 0, 0, 0, ...
int arr[30];
memset(arr, 1, sizeof(arr)); // That doesn't work correctly for arrays with multi-byte
// types such as int
仅供参考,以这种方式对静态数组使用 memset的()
给出:
arr[0] = 0x01010101
arr[1] = 0x01010101
arr[2] = 0x01010101
另一种选择:
for(count = 0; count < 30; count++)
arr[count] = 1; // Yup, that does it, but it's two lines.
任何人有其他的想法?只要是C code,对解决方案没有限制。 (其他库都很好)
Anyone have other ideas? As long as it's C code, no limits on the solution. (other libs are fine)
推荐答案
这是一个GCC扩展:
int a[100] = {[0 ... 99] = 1};
这篇关于初始化int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文