简体   繁体   English

初始化单值的多维数组

[英]Initializing multidimensional array of single value

Can someone remind me of the syntax of assigning an array in C, specifically multidimensional, to a single value? 有人能让我想起将C中的数组(特别是多维)分配给单个值的语法吗?

I believed the squiggly brackets did this, though I received a compiler error when I tested: 我相信这些波浪形的括号做了这个,虽然我在测试时遇到了编译器错误:

int ArrayName [5][5] = {1};

to initialize the array to 1. 将数组初始化为1。

There's no compact syntax in C language that would initialize all elements of an array to a single value, regardless of whether the array is multi-dimensional or single-dimensional. C语言中没有紧凑的语法可以将数组的所有元素初始化为单个值,无论数组是多维还是单维。 There's syntax that would set all elements to zero specifically (and it is = { 0 } ), but for any other value it is impossible. 有一种语法可以将所有元素专门设置为零(并且它是= { 0 } ),但对于任何其他值,它是不可能的。

Of course, you can spell out individual initializers for all array elements, but that's probably not what you are looking for. 当然,你可以为所有数组元素拼出单独的初始值设定项,但这可能不是你想要的。

All you can do is to set all elements to a specific value manually, by using assignment. 您所能做的就是通过使用赋值手动将所有元素设置为特定值。

I'm not sure this is possible at all. 我不确定这是否可行。 You should use for or while loops. 您应该使用forwhile循环。

You can use memset function if you want to fill memory with a single byte. 如果要用单个字节填充内存,可以使用memset函数。

Multi-dimensional arrays can be built dynamically using pointers-to-pointers with malloc. 可以使用malloc指针指向动态构建多维数组。 In the example below, a single pointer-to-pointer-to-int is declared. 在下面的示例中,声明了一个指向指针指向int的指针。 Upon declaration, the pointer has no meaningful value. 声明后,指针没有任何有意义的值。 A call to mallac then requests that the pointer point to a block of nx valid memory units: 然后调用mallac请求指针指向nx个有效内存单元块:

x =  (int **) malloc(nx * sizeof(int *));

After this call, x now has a valid value; 在此调用之后,x现在具有有效值; specifically, the beginning address of a block of memory which contains nx pointers-to-int. 特别是,包含nx指针到int的内存块的起始地址。 Eachof these pointers to int is just a regular pointer, as we have seen many times. 这些指向int的指针只是一个常规指针,正如我们多次看到的那样。 They have yet to be initialized to anything meaningful, and the can each be accessed as either 它们尚未被初始化为任何有意义的东西,并且每个都可以被访问

x[0], x[1] ... x[nx-1], OR *x, *(x+1), *(x+2),
  ... *(x+nx-1).

To give eachof these pointers a meaningful value, we can call malloc for each of them, such as is accomplished by this loop: 为了给每个指针赋予一个有意义的值,我们可以为每个指针调用malloc,例如通过这个循环完成:

 for (i=0;i<nx;++i){
    x[i] = ( int * ) malloc( ny * sizeof(int));
  }

Note that we could have also said: 请注意,我们也可以说:

for (i=0;i<nx;++i){
    *(x+i) = ( int * ) malloc( ny * sizeof(int));
  }

Now that each pointer in our array of pointers points to a meaningful block of memory (each of size ny ints), we can assign values. 既然我们的指针数组中的每个指针都指向一个有意义的内存块(每个都是大小为ny int),我们就可以赋值。 To understand how values are assigned, consider the schematic below. 要了解如何分配值,请考虑下面的原理图。 You will need to study this very carefully until it is very clear what is going on. 你需要仔细研究这个问题,直到很清楚发生了什么。 This can be a little tricky but once you get the hang of it it's not so bad. 这可能有点棘手但是一旦掌握了它就不那么糟糕了。

  x[0] --->   | *x[0] | *x[0]+1 | *x[0] + 2 | ... | *x[0]+ny-1 |
  x[1] --->   | *x[1] | *x[1]+1 | *x[1] + 2 | ... | *x[1]+ny-1 |

  .
  .
  .
  x[nx-1] ---> | *x[nx-1] | *x[nx-1]+1 | *x[nx-1] + 2 | ... | *x[nx-1]+ny-1 |

This is equivalent to: 这相当于:

  x[0] --->   | *(*(x+0)+0) | *(*(x+0)+1) | *(*(x+0)+2) | ... | *(*(x+0)+ny-1) |
  x[1] --->   | *(*(x+1)+0) | *(*(x+1)+1) | *(*(x+1)+2) | ... | *(*(x+1)+ny-1) |
  .
  .
  .
  x[nx-1] --->   | *(*(x+nx-1)+0) | *(*(x+nx-1)+1) | *(*(x+nx-1)+2) | ... | *(*(x+nx-1)+ny-1) |

And this is equivalent to: 这相当于:

  x[0] --->   | x[0][0] | x[0][1] | x[0][2] | ... | x[0][ny-1] |
  x[1] --->   | x[1][0] | x[1][1] | x[1][2] | ... | x[1][ny-1] |
  .
  .
  .
  x[nx-1] ---> | x[nx-1][0] | x[nx-1][1] | x[nx-1][2] | ... | x[nx-1][ny-1] |

... given the important relation: ......鉴于重要的关系:

 *( *(x + i) + j) = *( x[i] + j) = x[i][j]

You can do this: 你可以这样做:

int ArrayName[5][5];

for(size_t i = 0; i < 5; i++)
    for(size_t i2 = 0; i2 < 5; i2++)
        ArrayName[i][i2] = 1;

Or to be more efficient: 或者更高效:

int ArrayName[5][5];

for(size_t i = 0, *ptr = ArrayName; i < (5*5); ++i, ++ptr)
    *ptr = 1;

If you were crazy enough, you'd create a function for it: 如果你足够疯狂,你可以为它创建一个函数:

void init_arr(void* ptr, void* value, size_t size)
{
    for(size_t i = 0; i < size; ++i, ++ptr)
        *ptr = *value;
}

int ArrayName[5][5];
int val = 1;
init_arr(ArrayName, val, 5 * 5);

If you were using C++, you might use templates: 如果您使用的是C ++,则可以使用模板:

template <class T>
void init_arr(T *ptr, T value, size_t size)
{
    for(size_t i = 0; i < size; ++i, ++ptr)
        *ptr = value;
}

int ArrayName[5][5];
init_arr(ArrayName, 1, 5 * 5);

If you really were using C++, you'd use vectors... Heck, there's probably a fancy boost way to do this. 如果你真的在使用C ++,你会使用矢量......哎呀,这可能是一种花哨的提升方式。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM