0
int x=2,y=2;
int matrix[x][y];

if I want to set to '1' each element of the array, how can I do? then, if I want to set to zero the elements of the row 2, ho can I do? it seems easy, but I don't undestand how specify the length of row or of the array.

3
  • 1
    You need to understand how to access a 1D array. Then you can easily understand 2D array. Commented Feb 10, 2012 at 3:22
  • 1
    i suggest you search for c multidimensional array initialization in google. Commented Feb 10, 2012 at 3:22
  • Every played battleships? How do you specify a location there? Commented Feb 10, 2012 at 4:25

4 Answers 4

1

memset will put the same thing in every byte of the array - not in every int member of it, so you can use it for 0, but not for 1. Through this, I recommend to use loops even for 0.

Usually, when working with arrays, you need loops. When you work with 2D array (which is nested array), you need nested loops.

int i,j;
//put 1 in every member
for (i=0;i<x;i++)
 for (j=0;j<y;j++)
  matrix[i][j]=1;
//put 0 in the 2nd line
for (j=0;j<y;j++)
 matrix[1][j]=0;
Sign up to request clarification or add additional context in comments.

Comments

1

To initialize the array, as in, have the compiler set all the values when the variable is created, there is only one way:

int matrix[x][y] = 
{
  {1,1},
  {1,1}
};

However, there is a special rule in C allowing you to set every array item to zero, just by typing:

int matrix[x][y] = { 0 };

This one works with zeroes, if you try with any other value, only the first item of the array gets set.

Then of course there are various ways to set the items of the array in runtime, as shown in other answers, but that is assignment not initialization.

EDIT :

Note that the above is only true for "traditional", statically allocated arrays. If you are using the array declaration feature called "variable length arrays", you can only set its values in runtme.

A variable length array (vla) is an array that can have its bounds determined in runtime. In the original question, the array length was determined by the non-constant variables x and y, in runtime. Since the array length was determined in runtime, all initialization has to be done in runtime as well (with memset() etc).

Please note that vla is only available in the newer C standards C99 and C11!

2 Comments

in his example matrix is a vla and vla cannot be initialized
@ouah Aah yeah I didn't think of that. Thanks, will edit my post to clarify.
0

Your best bet to initialize an entire array is to use memset(). You'll need to use sizeof() to get the exact size of the array; do not assume that you know how many bytes there are in an int! You'll need to pass a a pointer, a "character", and a size to memset(). Warning: memset() is a sharp-edged tool. It will attempt to set the lump of memory to the value you specify, whether or not your process owns that memory. Warning 2: Always, always check the returned values from functions. In the case of memset(), on success it should be the same address (pointer) that was passed.

Here's how I'd do it:

#include <string.h>

char *lpMyMem;

lpMyMem = memset(matrix, (int) '\0', sizeof(int)*x*y;

if lpMyMem != matrix)
{
/* setting memory failed.  display message. */
return(-1); /* or another error code of your choosing! */
}

/* the rest of your program */

2 Comments

Your best bet to initialize an array to all zeros is the default initializer { 0 }, that's all.
I don't think that it works for var-length array. BTW, you can use sizeof(matrix) instead of sizeof(int)*x*y.
0
int x = 2, y = 2;
int matrix[x][y];

This declares a variable length array. Variable length arrays cannot be initialized: i.e.,

int x = 2, y = 2;
int matrix[x][y] = {0};  // not valid, matrix is a vla

if possible use constants for the dimensions, you'll be able to initialize matrix

#define X  2
#define Y  2
int matrix[X][Y] = {{1, 2}, {3, 4}};   // matrix is not a vla, ok

After initialization you can assign values to the array elements using memset or via assignment, element by element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.