Can someone remind me of the syntax of assigning an array in C, specifically multidimensional, to a single value?
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.
-
Further reading.Mateen Ulhaq– Mateen Ulhaq2011年04月11日 02:43:45 +00:00Commented Apr 11, 2011 at 2:43
-
1With squiggly braces you're supposed to provide a separate value for each member of the array (missing values are set to 0). I would go with Elalfer's answer since it's easy to understand. On a side note, if you have C++ available, vectors can be initialized as multiple copies of the same value (though this would probably be messy with multiple dimensions). Another side note: If you're allocating memory on the heap, you can use calloc instead of malloc to have the memory initialized to 0.Brandon Bohrer– Brandon Bohrer2011年04月11日 02:56:21 +00:00Commented Apr 11, 2011 at 2:56
4 Answers 4
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. There's syntax that would set all elements to zero specifically (and it is = { 0 }), but for any other value it is impossible.
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.
Comments
I'm not sure this is possible at all. You should use for or while loops.
You can use memset function if you want to fill memory with a single byte.
1 Comment
int ArrayName[5][5] = {1}; is legal, but it won't do what you expect.Multi-dimensional arrays can be built dynamically using pointers-to-pointers with malloc. In the example below, a single pointer-to-pointer-to-int is declared. 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:
x = (int **) malloc(nx * sizeof(int *));
After this call, x now has a valid value; specifically, the beginning address of a block of memory which contains nx pointers-to-int. Eachof these pointers to int is just a regular pointer, as we have seen many times. 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:
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. 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]
Comments
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:
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. :)