void pointer

Discussion in 'C Programming' started by harshal, Jan 24, 2008.

  1. harshal

    harshal Guest

    Hi all,
    i have 2 structures say struct a, and struct b

    i am in a function now in this function i want to store these 2
    pointers
    in global memory and store that address in void pointer.
    void abc(void *temp) {
    struct a * aptr;
    struct b * bptr;

    // get these pointers filed from temp
    aptr = (struct a *) temp;
    temp = temp + (sizeof(struct a *));
    bptr = temp;
    // i am confused about this.
    }

    void collect(struct collector *e) {
    struct a *aptr = NULL;
    struct b *bptr = NULL;
    void * temp = NULL;
    // suppose these two pointers are filled with appropriate values by
    iterating a list aptr and bptr
    // are ok now
    temp = malloc(2 * sizeof(void *));
    temp = aptr;
    temp = temp + sizeof(struct a *);
    temp = bptr;
    //then decrement temp again to get original value
    temp = temp - sizeof(struct a *);
    //and pass this pointer to other function i dont want to send them
    as an parameter
    abc(temp);
    }

    is this right??
    i forgot everything about void pointers but i know that we can not do
    arithmetic operations on void ptr and if we do it will be like char
    ptr and we can not dereference it also.
    and please guide me through how abc will collect these two pointers in
    local variables also

    Thanks
    harshal, Jan 24, 2008
    #1
    1. Advertising

  2. harshal

    Mark Bluemel Guest

    harshal wrote:
    > Hi all,
    > i have 2 structures say struct a, and struct b


    <Snip>
    >
    > void collect(struct collector *e) {
    > struct a *aptr = NULL;
    > struct b *bptr = NULL;
    > void * temp = NULL;
    > // suppose these two pointers are filled with appropriate values by
    > iterating a list aptr and bptr


    That illustrates why you shouldn't use '//' comments in newsgroup
    postings.

    > // are ok now
    > temp = malloc(2 * sizeof(void *));
    > temp = aptr;


    Surely you mean "*temp = aptr;" ?

    > temp = temp + sizeof(struct a *);
    > temp = bptr;


    and again "*temp = bptr;"

    > //then decrement temp again to get original value
    > temp = temp - sizeof(struct a *);
    > //and pass this pointer to other function i dont want to send them
    > as an parameter
    > abc(temp);
    > }
    >


    You don't half make things more complicated than they need to be...

    What's wrong with :-

    void abc(void *list_of_pointers[]) {
    struct a * aptr = list_of_pointers[0];
    struct b * bptr = list_of_po;
    /* use the pointers ... */
    }

    void collect(struct collector *e) {
    struct a *aptr = NULL;
    struct b *bptr = NULL;
    /* suppose these two pointers are filled
    * with appropriate values by iterating
    * a list, so aptr and bptr are ok now
    */
    void * temp = malloc(2 * sizeof(void *));
    temp[0] = aptr;
    temp[1] = bptr;
    abc(temp);
    }
    Mark Bluemel, Jan 24, 2008
    #2
    1. Advertising

  3. harshal

    Mark Bluemel Guest

    Mark Bluemel wrote:
    > harshal wrote:
    >> Hi all,
    >> i have 2 structures say struct a, and struct b

    >
    > <Snip>
    >>
    >> void collect(struct collector *e) {
    >> struct a *aptr = NULL;
    >> struct b *bptr = NULL;
    >> void * temp = NULL;
    >> // suppose these two pointers are filled with appropriate values by
    >> iterating a list aptr and bptr

    >
    > That illustrates why you shouldn't use '//' comments in newsgroup
    > postings.
    >
    >> // are ok now
    >> temp = malloc(2 * sizeof(void *));
    >> temp = aptr;

    >
    > Surely you mean "*temp = aptr;" ?
    >
    >> temp = temp + sizeof(struct a *);
    >> temp = bptr;

    >
    > and again "*temp = bptr;"
    >
    >> //then decrement temp again to get original value
    >> temp = temp - sizeof(struct a *);
    >> //and pass this pointer to other function i dont want to send them
    >> as an parameter
    >> abc(temp);
    >> }
    >>

    >
    > You don't half make things more complicated than they need to be...
    >
    > What's wrong with :-
    >
    > void abc(void *list_of_pointers[]) {
    > struct a * aptr = list_of_pointers[0];

    [Corrected below]
    > struct b * bptr = list_of_pointers[1];
    > /* use the pointers ... */
    > }
    >
    > void collect(struct collector *e) {
    > struct a *aptr = NULL;
    > struct b *bptr = NULL;
    > /* suppose these two pointers are filled
    > * with appropriate values by iterating
    > * a list, so aptr and bptr are ok now
    > */

    [Corrected below]
    > void ** temp = malloc(2 * sizeof(void *));
    > temp[0] = aptr;
    > temp[1] = bptr;
    > abc(temp);
    > }
    Mark Bluemel, Jan 24, 2008
    #3
  4. harshal

    Army1987 Guest

    harshal wrote:

    > Hi all,
    > i have 2 structures say struct a, and struct b
    >
    > i am in a function now in this function i want to store these 2
    > pointers
    > in global memory and store that address in void pointer.
    > void abc(void *temp) {
    > struct a * aptr;
    > struct b * bptr;
    >
    > // get these pointers filed from temp
    > aptr = (struct a *) temp;
    > temp = temp + (sizeof(struct a *));
    > bptr = temp;

    aptr and bptr are automatic variables of abc, and will die as soon as it
    returns. That is not what you wanted to do, I guess.

    > // i am confused about this.
    > }
    >
    > void collect(struct collector *e) {
    > struct a *aptr = NULL;
    > struct b *bptr = NULL;
    > void * temp = NULL;
    > // suppose these two pointers are filled with appropriate values by
    > iterating a list aptr and bptr

    Long lines are likely to be wrapped in Usenet, breaking // comments. Use
    them only on very short lines (or better, don't use them at all, for
    compatibility with C89 which didn't have them).

    > // are ok now
    > temp = malloc(2 * sizeof(void *));
    > temp = aptr;

    This immediately throws away the memory you allocated.
    > temp = temp + sizeof(struct a *);
    > temp = bptr;

    If you wanted to store two pointers in temp, just declare it as void **, so
    that you can do:
    struct a *aptr;
    struct b *bptr;
    void **temp;
    /* fill aptr and bptr */
    temp = malloc(2 * sizeof *temp);
    if (temp == NULL) {
    /* memory allocation failed, do something */
    }
    temp[0] = aptr;
    temp[1] = bptr;

    > //then decrement temp again to get original value temp = temp -
    > sizeof(struct a *);
    > //and pass this pointer to other function i dont want to send them
    > as an parameter
    > abc(temp);
    > }
    >
    > is this right??
    > i forgot everything about void pointers but i know that we can not do
    > arithmetic operations on void ptr and if we do it will be like char ptr
    > and we can not dereference it also. and please guide me through how abc
    > will collect these two pointers in local variables also


    declare abc as taking a void **:
    void abc(void **temp)
    {
    struct a *aptr = temp[0];
    struct b *bptr = temp[1];
    /* do what you need with them */
    }


    --
    Army1987 (Replace "NOSPAM" with "email")
    Army1987, Jan 24, 2008
    #4
  5. Mark Bluemel <> writes:
    > harshal wrote:
    >> i have 2 structures say struct a, and struct b

    >
    > <Snip>
    >>
    >> void collect(struct collector *e) {
    >> struct a *aptr = NULL;
    >> struct b *bptr = NULL;
    >> void * temp = NULL;
    >> // suppose these two pointers are filled with appropriate values by
    >> iterating a list aptr and bptr

    >
    > That illustrates why you shouldn't use '//' comments in newsgroup
    > postings.
    >
    >> // are ok now
    >> temp = malloc(2 * sizeof(void *));
    >> temp = aptr;

    >
    > Surely you mean "*temp = aptr;" ?


    Surely not. temp is of type void*; what do you expect *temp to be?

    [snip]

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 24, 2008
    #5
    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. Ollej Reemt
    Replies:
    7
    Views:
    527
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    788
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    834
    S.Tobias
    Jul 22, 2005
  4. Hakirato
    Replies:
    4
    Views:
    882
    Alf P. Steinbach
    Oct 5, 2006
  5. Replies:
    1
    Views:
    406
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page