我在C中有一个大数组(不是c++,如果这有区别的话)。我想初始化相同值的所有成员。
我发誓我曾经知道一个简单的方法。在我的情况下,我可以使用memset(),但没有一种方法来做到这一点,这是建立在C语法?
我在C中有一个大数组(不是c++,如果这有区别的话)。我想初始化相同值的所有成员。
我发誓我曾经知道一个简单的方法。在我的情况下,我可以使用memset(),但没有一种方法来做到这一点,这是建立在C语法?
当前回答
我知道用户Tarski以类似的方式回答了这个问题,但我添加了更多细节。请原谅我的一些C语言,因为我对它有点生疏,因为我更倾向于使用c++,但它就在这里。
如果你提前知道数组的大小……
#include <stdio.h>
typedef const unsigned int cUINT;
typedef unsigned int UINT;
cUINT size = 10;
cUINT initVal = 5;
void arrayInitializer( UINT* myArray, cUINT size, cUINT initVal );
void printArray( UINT* myArray );
int main() {
UINT myArray[size];
/* Not initialized during declaration but can be
initialized using a function for the appropriate TYPE*/
arrayInitializer( myArray, size, initVal );
printArray( myArray );
return 0;
}
void arrayInitializer( UINT* myArray, cUINT size, cUINT initVal ) {
for ( UINT n = 0; n < size; n++ ) {
myArray[n] = initVal;
}
}
void printArray( UINT* myArray ) {
printf( "myArray = { " );
for ( UINT n = 0; n < size; n++ ) {
printf( "%u", myArray[n] );
if ( n < size-1 )
printf( ", " );
}
printf( " }\n" );
}
上面有一些注意事项;一个是UINT myArray[size];声明时不会直接初始化,但是下一个代码块或函数调用确实会将数组的每个元素初始化为所需的相同值。另一个注意事项是,您必须为将要支持的每种类型编写初始化函数,并且还必须修改printArray()函数以支持这些类型。
您可以使用这里找到的在线编译器尝试此代码。
其他回答
对于初始化“普通”数据类型(如int数组),你可以使用括号符号,但如果数组中仍有空格,它将把最后一个后面的值归零:
// put values 1-8, then two zeroes
int list[10] = {1,2,3,4,5,6,7,8};
#include<stdio.h>
int main(){
int i,a[50];
for (i=0;i<50;i++){
a[i]=5;// set value 5 to all the array index
}
for (i=0;i<50;i++)
printf("%d\n",a[i]);
return 0;
}
它将给o/p 5 5 5 5 5 5 ......直到整个数组的大小
通过所有的喋喋不休,简短的回答是,如果你在编译时打开优化,你不会做得比下面更好:
int i,value=5,array[1000];
for(i=0;i<1000;i++) array[i]=value;
额外的好处:代码实际上是易读的:)
我知道最初的问题明确地提到了C而不是c++,但如果你(像我一样)来这里寻找c++数组的解决方案,这里有一个巧妙的技巧:
如果你的编译器支持fold表达式,你可以使用template magic和std::index_sequence生成一个包含你想要的值的初始化列表。你甚至可以把它想象成一个老板:
#include <array>
/// [3]
/// This functions's only purpose is to ignore the index given as the second
/// template argument and to always produce the value passed in.
template<class T, size_t /*ignored*/>
constexpr T identity_func(const T& value) {
return value;
}
/// [2]
/// At this point, we have a list of indices that we can unfold
/// into an initializer list using the `identity_func` above.
template<class T, size_t... Indices>
constexpr std::array<T, sizeof...(Indices)>
make_array_of_impl(const T& value, std::index_sequence<Indices...>) {
return {identity_func<T, Indices>(value)...};
}
/// [1]
/// This is the user-facing function.
/// The template arguments are swapped compared to the order used
/// for std::array, this way we can let the compiler infer the type
/// from the given value but still define it explicitly if we want to.
template<size_t Size, class T>
constexpr std::array<T, Size>
make_array_of(const T& value) {
using Indices = std::make_index_sequence<Size>;
return make_array_of_impl(value, Indices{});
}
// std::array<int, 4>{42, 42, 42, 42}
constexpr auto test_array = make_array_of<4/*, int*/>(42);
static_assert(test_array[0] == 42);
static_assert(test_array[1] == 42);
static_assert(test_array[2] == 42);
static_assert(test_array[3] == 42);
// static_assert(test_array[4] == 42); out of bounds
您可以查看工作中的代码(在Wandbox中)
我知道用户Tarski以类似的方式回答了这个问题,但我添加了更多细节。请原谅我的一些C语言,因为我对它有点生疏,因为我更倾向于使用c++,但它就在这里。
如果你提前知道数组的大小……
#include <stdio.h>
typedef const unsigned int cUINT;
typedef unsigned int UINT;
cUINT size = 10;
cUINT initVal = 5;
void arrayInitializer( UINT* myArray, cUINT size, cUINT initVal );
void printArray( UINT* myArray );
int main() {
UINT myArray[size];
/* Not initialized during declaration but can be
initialized using a function for the appropriate TYPE*/
arrayInitializer( myArray, size, initVal );
printArray( myArray );
return 0;
}
void arrayInitializer( UINT* myArray, cUINT size, cUINT initVal ) {
for ( UINT n = 0; n < size; n++ ) {
myArray[n] = initVal;
}
}
void printArray( UINT* myArray ) {
printf( "myArray = { " );
for ( UINT n = 0; n < size; n++ ) {
printf( "%u", myArray[n] );
if ( n < size-1 )
printf( ", " );
}
printf( " }\n" );
}
上面有一些注意事项;一个是UINT myArray[size];声明时不会直接初始化,但是下一个代码块或函数调用确实会将数组的每个元素初始化为所需的相同值。另一个注意事项是,您必须为将要支持的每种类型编写初始化函数,并且还必须修改printArray()函数以支持这些类型。
您可以使用这里找到的在线编译器尝试此代码。