我在C中有一个大数组(不是c++,如果这有区别的话)。我想初始化相同值的所有成员。

我发誓我曾经知道一个简单的方法。在我的情况下,我可以使用memset(),但没有一种方法来做到这一点,这是建立在C语法?


当前回答

没有人提到访问初始化数组元素的索引顺序。我的示例代码将为它提供一个说明性示例。

#include <iostream>

void PrintArray(int a[3][3])
{
    std::cout << "a11 = " << a[0][0] << "\t\t" << "a12 = " << a[0][1] << "\t\t" << "a13 = " << a[0][2] << std::endl;
    std::cout << "a21 = " << a[1][0] << "\t\t" << "a22 = " << a[1][1] << "\t\t" << "a23 = " << a[1][2] << std::endl;
    std::cout << "a31 = " << a[2][0] << "\t\t" << "a32 = " << a[2][1] << "\t\t" << "a33 = " << a[2][2] << std::endl;
    std::cout << std::endl;
}

int wmain(int argc, wchar_t * argv[])
{
    int a1[3][3] =  {   11,     12,     13,     // The most
                        21,     22,     23,     // basic
                        31,     32,     33  };  // format.

    int a2[][3] =   {   11,     12,     13,     // The first (outer) dimension
                        21,     22,     23,     // may be omitted. The compiler
                        31,     32,     33  };  // will automatically deduce it.

    int a3[3][3] =  {   {11,    12,     13},    // The elements of each
                        {21,    22,     23},    // second (inner) dimension
                        {31,    32,     33} };  // can be grouped together.

    int a4[][3] =   {   {11,    12,     13},    // Again, the first dimension
                        {21,    22,     23},    // can be omitted when the 
                        {31,    32,     33} };  // inner elements are grouped.

    PrintArray(a1);
    PrintArray(a2);
    PrintArray(a3);
    PrintArray(a4);

    // This part shows in which order the elements are stored in the memory.
    int * b = (int *) a1;   // The output is the same for the all four arrays.
    for (int i=0; i<9; i++)
    {
        std::cout << b[i] << '\t';
    }

    return 0;
}

输出结果为:

a11 = 11                a12 = 12                a13 = 13
a21 = 21                a22 = 22                a23 = 23
a31 = 31                a32 = 32                a33 = 33

a11 = 11                a12 = 12                a13 = 13
a21 = 21                a22 = 22                a23 = 23
a31 = 31                a32 = 32                a33 = 33

a11 = 11                a12 = 12                a13 = 13
a21 = 21                a22 = 22                a23 = 23
a31 = 31                a32 = 32                a33 = 33

a11 = 11                a12 = 12                a13 = 13
a21 = 21                a22 = 22                a23 = 23
a31 = 31                a32 = 32                a33 = 33

11      12      13      21      22      23      31      32      33

其他回答

你可以像上面详细描述的那样做整个静态初始化器的事情,但是当你的数组大小发生变化时(当你的数组变大时,如果你没有添加适当的额外初始化器,你就会得到垃圾),这可能是一个真正的麻烦。

Memset为您的工作提供了一个运行时命中,但是任何正确的代码大小命中都不受数组大小变化的影响。当数组大于几十个元素时,我几乎在所有情况下都会使用这个解决方案。

如果静态声明数组真的很重要,我会写一个程序来为我编写程序,并使其成为构建过程的一部分。

用0初始化

  char arr[1000] = { 0 };

最好使用普通的“for循环”来进行除0以外的初始化。

  char arr[1000];
  for(int i=0; i<arr.size(); i++){
     arr[i] = 'A';
  }

通过所有的喋喋不休,简短的回答是,如果你在编译时打开优化,你不会做得比下面更好:

int i,value=5,array[1000]; 
for(i=0;i<1000;i++) array[i]=value; 

额外的好处:代码实际上是易读的:)

如果数组恰好是int或int大小的任何东西,或者你的mempattern的大小恰好符合int的精确时间(即全部为零或0xA5A5A5A5),最好的方法是使用memset()。

否则,在循环中调用memcpy()移动索引。

在过去(我并不是说这是个好主意),我们设置了第一个元素,然后:

Memcpy (&element [1], &element [0], sizeof (element)-sizeof (element [0]);

甚至不确定它是否还能工作(这将取决于memcpy的实现),但它通过重复地将初始元素复制到下一个元素来工作-甚至对结构数组也有效。