initialize two dimension array of pointers using memset

D

davoti

Hi,

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,,, appreciate some advise.

Thanks
 
K

Keith Thompson

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,,, appreciate some advise.

Not reliably, no. That will initialize the array to all-bits-zero,
which will set all the pointers to NULL only if all-bits-zero is a
representation of a null pointer. It is in many, probably most,
implementations, but it's not guaranteed.

However, you can initialize all the pointers to null:

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

Incidentally, the names MAX_COL and MAX_ROW might be misleading.
If MAX_COL is 100, the maximum column is 99.
 
T

Tomás Ó hÉilidhe

 That will initialize the array to all-bits-zero,
which will set all the pointers to NULL only if all-bits-zero is a
representation of a null pointer.  It is in many, probably most,
implementations, but it's not guaranteed.


Out of curiosity, has anyone ever encountered an implementation on
which the null pointer wasn't all bits zero?
 
T

Tomás Ó hÉilidhe

typedef struct my_s {
   int x;
   int y;

} my_T;



There's no harm in giving them the same name:

typedef struct Vitamin {

} Vitamin;


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,,, appreciate some advise.


This array you've created is of static duration, so you could have
just used an initialiser to make it all null pointers.

However, if it's dynamic, you might have:

#include <stddef.h> /* size_t */

int main(void)
{
your_T (*const p_array)[MAX_COL][MAX_ROW] = malloc(sizeof *p);

if (!p_array)
return 0;

unsigned i, unsigned j;

for (i = 0; i != MAX_COL; ++i)
{
for (j = 0; j != MAX_ROW; ++j)
{
(*p_array)[j] = 0;
}
}

free(p_array);
}

If you get sick of dereferencing it all the time, you can do:

#define array (*p_array)

/* Code goes here */

#undef array
 
H

Harald van Dijk

Out of curiosity, has anyone ever encountered an implementation on which
the null pointer wasn't all bits zero?

Yes, tendra has an option to use 0x55555555 as the representation for
null pointers on x86. Of course, make sure not to mix libraries compiled
with and without this option.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top