简体   繁体   中英

initialize two dimensional array of pointer elements using memset

I have a these structures 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 the array's pointer to NULL, can I do:

memset (array, 0, sizeof(array))

this does not look right to me.

最简单的是

your_T array[MAX_COL][MAX_ROW] = {{{0}}};
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 the pointers of your_T to null using memset, because it is not specified that a null pointer has its bit pattern all consisting of null bits. But you can create your array like this:

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

The elements will be default initialized, which means for a pointer that the pointer will contain a null pointer value. If your array is global, you don't even have to care. That will happen by default then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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