Structures, Pointer Elements, and Dereferencing

This is a discussion on Structures, Pointer Elements, and Dereferencing within the C Programming forums, part of the General Programming Boards category; I'm having an issue determining the appropriate dereferencing method to get the address of a pointer contained inside of a ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Structures, Pointer Elements, and Dereferencing

    I'm having an issue determining the appropriate dereferencing method to get the address of a pointer contained inside of a structure. Pointers were never my strong point. The structure might look like this:

    Code:
    struct myStruct {
            void* myPtr;
            int somecount;
    };
    Now the issue is that I am attempting to access a linked list that acts like an imprint of some data. So the access is like:

    Code:
    struct myStruct* myList;
    struct myStruct* mySearch;
    void* somePtr=/*assigned some address */
    
    // create the link list with myList and fill as appropriate
    mySearch=&myList;
    while( mySearch != NULL ) {
           // now here is where my problem is...
           // how would you 'word' this... if( mySearch->(&myPtr) == &somePtr ) {   ???
           else { mySearch=mySearch->n; }
    }
    Maybe I have it right, I'm not entirely sure. The code isn't at a stage I can test it, but due to my lack in strength with pointers I figured if it was going to strike it will be with a line exactly like this. I tried searching the web, but all I got were tutorials on pointers. None of which decided to even glance at a use of pointers like this. Incase the line isn't self-explanatory (like I think it is lol) its intended to compare two pointer addresses to make sure they are pointing to the same "chunk", the problem of course being that one is sitting inside of a pointer to a structure. The use of '&' and other similar pointer accesses in conjunction with structure referencing with -> and ., if any of you happen to have a good link for this please share Would be greatly appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should re-read those pointer tutorials and note the complete absence of & when using pointers. If you want the value of mySearch->myPtr, it is in fact mySearch->myPtr. There are no & involved. (Edit: I mean of course on the pointers themselves. There may be & elsewhere in the code, naturally.)
    Last edited by tabstop; 05-22-2009 at 11:51 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The member access will be performed first, so you apply the address-of operator to everything:
    Code:
    &mySearch->myPtr
    Which is equivalent to this:
    Code:
    &(mySearch->myPtr)
    Also note that a pointer to a pointer to void (void**) often behaves differently from what people expect, if that's what you were going for.
    My best code is written with the delete key.

  4. #4
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Thanks both of you.
    Tabstop ya I said my work with pointers sucked. I went back and read, and now that realization might actually help me out a lot, so I guess I was trying to get the address of the pointer itself instead of attempting to get the address it pointed to. If I'm not mistaken again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dereferencing Class Pointer List
    By toonlover in forum C++ Programming
    Replies: 22
    Last Post: 08-16-2008, 08:39 PM
  2. pointer probs. with structs.
    By dinjas in forum C Programming
    Replies: 6
    Last Post: 03-08-2005, 05:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21