passing a pointer to initialized structrue in a function

Discussion in 'C Programming' started by ArifulHossain tuhin, Mar 2, 2012.

  1. I have this structure like following :

    typedef struct{
    char static_data[10];
    int a;
    int b;
    } my_ds;

    i have initialized it like following:

    my_ds * ds = (my_ds *)malloc(sizeof(my_ds));
    memmove(my_ds->static_data, buf, len);
    ds->a = c;
    ds->b = d;

    and passed it in a function:

    int my_fun(void ** data);
    my_fun((void *)&ds);

    what happens inside my_fun, the first field, static[10] is initialized correctly but other two values are zero. What am i missing here?
    ArifulHossain tuhin, Mar 2, 2012
    #1
    1. Advertising

  2. On Friday, March 2, 2012 9:39:25 PM UTC+6, ArifulHossain tuhin wrote:
    > I have this structure like following :
    >
    > typedef struct{
    > char static_data[10];
    > int a;
    > int b;
    > } my_ds;
    >
    > i have initialized it like following:
    >
    > my_ds * ds = (my_ds *)malloc(sizeof(my_ds));
    > memmove(my_ds->static_data, buf, len);
    > ds->a = c;
    > ds->b = d;
    >
    > and passed it in a function:
    >
    > int my_fun(void ** data);
    > my_fun((void *)&ds);
    >
    > what happens inside my_fun, the first field, static[10] is initialized correctly but other two values are zero. What am i missing here?


    Sorry for dual posting. first one was not shown for a while. so i thought i did something wrong and posted again. group admin can delete one.
    ArifulHossain tuhin, Mar 2, 2012
    #2
    1. Advertising

  3. ArifulHossain tuhin

    James Kuyper Guest

    On 03/02/2012 10:44 AM, ArifulHossain tuhin wrote:
    ....
    > Sorry for dual posting. first one was not shown for a while. so i thought i did something wrong and posted again. group admin can delete one.


    There is no group administrator. Since you used google groups to post
    it, you can view the message in google groups, and then select the
    appropriate option (I don't remember what it was) to delete the message.
    That will remove it from Google's archives, and may cause a request to
    be sent out to other news servers asking them to remove their copies of
    the message as well. However, such requests are routinely ignored by
    most news servers, because they are so easily faked and have so
    frequently been abused.

    Basically, once you post to usenet, copies of your message are extremely
    unlikely to ever completely disappear (unless you don't want them to -
    Murphy rules).
    James Kuyper, Mar 2, 2012
    #3
  4. ArifulHossain tuhin

    James Kuyper Guest

    On 03/02/2012 11:45 AM, James Kuyper wrote:
    ....
    > void *temp = &ds;


    Correction:
    void *temp = ds;

    > my_fun(&temp);
    James Kuyper, Mar 2, 2012
    #4
  5. James Kuyper <> writes:

    > On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:

    <snip>
    >> my_ds * ds = (my_ds *)malloc(sizeof(my_ds));

    <snip>
    >> memmove(my_ds->static_data, buf, len);
    >> ds->a = c;
    >> ds->b = d;
    >>
    >> and passed it in a function:
    >>
    >> int my_fun(void ** data);
    >> my_fun((void *)&ds);

    >
    > ds object of type my_ds, and &ds is a pointer to that object.


    You've missed a level of indirection (or maybe just several words got
    lost from the sentence). ds is an object of type pointer to my_ds. &ds
    is indeed a pointer to "that object", but it's type is pointer to
    pointer to my_ds.

    <snip>
    --
    Ben.
    Ben Bacarisse, Mar 2, 2012
    #5
  6. ArifulHossain tuhin

    Ike Naar Guest

    On 2012-03-02, ArifulHossain tuhin <> wrote:
    > I have this structure like following :
    >
    > typedef struct{
    > char static_data[10];
    > int a;
    > int b;
    > } my_ds;
    >
    > i have initialized it like following:
    >
    > my_ds * ds = (my_ds *)malloc(sizeof(my_ds));
    > memmove(my_ds->static_data, buf, len);


    Did you mean

    memmove(ds->static_data, buf, len);

    here ?

    > ds->a = c;
    > ds->b = d;
    >
    > and passed it in a function:
    >
    > int my_fun(void ** data);
    > my_fun((void *)&ds);
    >
    > what happens inside my_fun, the first field, static[10] is initialized
    > correctly but other two values are zero. What am i missing here?


    Apart from a few stylistic things, I don't see problems in the code that
    could explain the behaviour (assuming c and d were nonzero, and that
    len <= 10).
    Can you show a *complete* small program (one that we can compile and run
    without having to guess about the missing parts) that exhibits the faulty
    behaviour?
    Ike Naar, Mar 2, 2012
    #6
  7. ArifulHossain tuhin

    James Kuyper Guest

    On 03/02/2012 12:02 PM, Ben Bacarisse wrote:
    > James Kuyper <> writes:
    >
    >> On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:

    > <snip>
    >>> my_ds * ds = (my_ds *)malloc(sizeof(my_ds));

    ....
    >> ds object of type my_ds, and &ds is a pointer to that object.

    >
    > You've missed a level of indirection (or maybe just several words got
    > lost from the sentence).


    I did lose a level of indirection (without, oddly enough, invalidating
    my conclusion). I posted a correction to my code, but I should have
    corrected the text, too:

    "ds is pointer to an object of type my_ds, and &ds is a pointer to that
    pointer."
    James Kuyper, Mar 2, 2012
    #7
    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. James Vanns
    Replies:
    7
    Views:
    7,011
    Evan Carew
    Jan 21, 2004
  2. beetle
    Replies:
    2
    Views:
    884
    beetle
    Jan 25, 2005
  3. luciferleo
    Replies:
    2
    Views:
    314
    luciferleo
    Dec 18, 2005
  4. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  5. Vijai Kalyan
    Replies:
    4
    Views:
    689
    Vijai Kalyan
    Nov 8, 2005
Loading...

Share This Page