Initializing a multi-dimensional array of a single value

Can someone remind me of the syntax for assigning an array in C, namely multidimensional, to a single value?

I figured sliding brackets did this, although I got a compiler error when I tested:

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

to initialize the array to 1.

+4
source share
4 answers

C does not have a compact syntax that initializes all elements of an array to a single value, regardless of whether the array is multidimensional or one-dimensional. There is a syntax that will set all elements to zero on purpose (and this = { 0 } ), but for any other value this is not possible.

Of course, you can specify separate initializers for all elements of the array, but this is probably not what you are looking for.

All you can do is set all the elements to a specific value manually using the assignment.

+6
source

I'm not sure if this is possible at all. You should use for or while loops.

You can use the memset function if you want to fill the memory with one byte.

+1
source

Multidimensional arrays can be built dynamically using pointers to pointers with malloc. In the example below, pointer-to-pointer-to-int is declared. After the declaration, the pointer does not matter. Then a call to mallac asks that the pointer points to a block of nx valid memory blocks:

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

After this call, x now has a valid value; in particular, the starting address of a memory block that contains nx pointers to international. Each of these pointers to int is a regular pointer, as we have seen many times. They are not yet initialized to any significant, and each of them can be accessed as either

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

To give each of these pointers a meaningful value, we can call malloc for each of them, for example, this loop:

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

Please note that we could also say:

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

Now that every pointer in our array of pointers points to a significant block of memory (each of the sizes is ny ints), we can assign values. To understand how values ​​are assigned, consider the diagram below. You will need to study this very carefully, while it is very clear what is happening. It may be a little complicated, but once you get it, it is 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 ratio:

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

You can do it:

 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 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 would 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 used C ++, you can use templates:

 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 used C ++, you would use vectors ... Damn it, maybe this can be a very interesting way to do this. :)

+1
source

Source: https://habr.com/ru/post/1347473/


All Articles