initialize two dimension array of pointers using memset

Discussion in 'C Programming' started by davoti@gmail.com, Dec 29, 2008.

  1. Guest

    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
     
    , Dec 29, 2008
    #1
    1. Advertising

  2. writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 29, 2008
    #2
    1. Advertising

  3. On Dec 29, 12:07 pm, Keith Thompson <> wrote:

    > 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?
     
    Tomás Ó hÉilidhe, Dec 29, 2008
    #3
  4. On Dec 29, 11:47 am, wrote:

    > 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
     
    Tomás Ó hÉilidhe, Dec 29, 2008
    #4
  5. On Sun, 28 Dec 2008 23:03:02 -0800, Tomás Ó hÉilidhe wrote:
    > On Dec 29, 12:07 pm, Keith Thompson <> wrote:
    >> 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?


    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.
     
    Harald van Dijk, Dec 29, 2008
    #5
  6. CBFalconer Guest

    Tomás Ó hÉilidhe wrote:
    > Keith Thompson <> wrote:
    >
    >> 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?


    Yes

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee, Frohe Weihnachten
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Dec 29, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Lindsey
    Replies:
    5
    Views:
    542
    Mike Wahler
    Sep 27, 2003
  2. Replies:
    12
    Views:
    594
    Thad Smith
    Oct 10, 2005
  3. Joakim Hove

    memset to initialize double array?

    Joakim Hove, Nov 8, 2005, in forum: C Programming
    Replies:
    3
    Views:
    863
    SM Ryan
    Nov 8, 2005
  4. silversurfer2025
    Replies:
    22
    Views:
    3,118
    Jerry Coffin
    Jul 29, 2006
  5. Luuk
    Replies:
    15
    Views:
    843
    Nobody
    Feb 11, 2010
Loading...

Share This Page