Initialize a two dimensional array of pointer elements using memset

I have these structure definitions

typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

      

To initialize an array pointer to NULL, can I do:

memset (array, 0, sizeof(array))

      

it doesn't look right.

+1


source to share


2 answers


easiest



your_T array[MAX_COL][MAX_ROW] = {{{0}}};

      

+3


source


typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

      

You cannot initialize your_T pointers to null using memset, because it is not specified that a null pointer has its own bit pattern of zero bits. But you can create your array like this:



your_T array[MAX_COL][MAX_ROW] = {{}};

      

The elements will be initialized by default, which means the pointer indicates that the pointer will contain a null pointer value. If your array is global you don't even need to care. Then this will happen by default.

+1


source







All Articles