有没有人有办法简单地将 s 数组int
(任何多字节类型都可以)初始化为非零和非 -1 值?我的意思是,有没有办法在一个班轮中做到这一点,而不必单独执行每个元素:
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 代码,解决方案就没有限制。(其他库都很好)