Pointers pointers pointers....

This is a discussion on Pointers pointers pointers.... within the C Programming forums, part of the General Programming Boards category; Hi, I am mucking around with pointers in a project and got a questions. The code below is not complete ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    55

    Pointers pointers pointers....

    Hi,

    I am mucking around with pointers in a project and got a questions. The code below is not complete but it illustrates what i dont get.

    main(){
    /* defined elsewhere */
    struct aStruct *f=NULL, **ff = NULL, ***fff = NULL;


    f = getStruct(); /* gets a struct using malloc */

    /* I dont get why i have to asign the address of the pointer
    since f already is a pointer and i though a pointer is a
    address and ff=f would do */

    ff = &f;
    (*fff)->x=54321;

    etc...

    /* Same here. I dont get why i have to asigne the address
    of the pointer since i though a pointer is a address and
    fff=ff would do */

    fff = &ff
    (**fff)->x=12345;
    etc....

    free(f);

    }

    G'n'R

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, what you don't understand (I think) is the levels of indirection. When you have:

    ff = &f;

    You have f (which is declared as *f) and ff (which is declared as **ff). Notice the 2 asterisks for ff. This tells you that you the level of indirection of 2 for ff. Now, if you want to assign, you have to match the levels of indirection. Since f only has one, you have to make a pointer out of a pointer (which is 2 levels of indirection). Then you can assign the value f to ff. Understand?

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99

    Smile

    to put it simply

    imagine

    struct aStruct e;
    struct aStruct *f = NULL, **ff = NULL....etc

    now to assignthe address of the structure e to f
    you can do
    f = &e;
    bcoz f is a pointer to the structure

    now ff is a pointer to the pointer of type aStruct so

    ff must contain a pointer to f not to e

    as would happen if you say ff = f(youmay pop up an error too(or a warning atleast)

    so you say ff = &f and assign the address where f is stored to ff

    and so on for fff

  5. #5
    Sayeh
    Guest
    Many times it is advantageous (necessary even) to work with the address of a pointer that points to another pointer or data block.

    Levels of indirection. It is called indirection because you are not working with the 'contents', but rather with an address of a memory location whose contents are the address of another memory location where either a pointer (another address) or a datablock (contents) reside.

    This can be useful, for example, in a memory manager where the O/S wants to be able to move blocks around (for compaction and general management/organization of RAM) without invalidating the pointer it has returned to the requester everytime it moves a block.

    To do this, instead of handing the requester a pointer to the RAMblock (an address to the block), it hands the requester a pointer to a pointer to a RAMblock. That is a double dereference (or double indirection).

    This way, the pointer that gets handed to the requester doesn't change, but the pointer that the memory manager is aware of does, because the block gets moved.

    If any of you know a developer at Microsoft, in the O/S group, please have them leave me a note and I will teach them this simple concept.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > If any of you know a developer at Microsoft, in the O/S group, please have them leave me a note and I will teach them this simple concept.

    Ohhh, nice! Low...but nice.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    Okey cool. Thanks. I think i am getting the picuture. But to clearify.

    struct aStruct *f=NULL, **ff=NULL, ***fff=NULL;


    ff = &f;

    Here i am asigning the address of the pointer f to ff. However ff still has its own address in memory, right? So i am pointing to a address which f holds, but at the same time ff has its own address.


    fff = &ff
    Here i am doing the same thing but with another level of indirection. e.g. I am asigning the address of ff which holds the address of f but fff still has its own address. So i actuall have two addresses "stored" in fff as well as fff having its own.

    hmm, that was probable a bad explanation of things , but am i getting close ??

    The rest of it i understand.

    G'n'R

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > hmm, that was probable a bad explanation of things , but am i getting close ??
    Looks good to me
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Everything has an address and contents.

    So, think like this:

    Code:
     Name | Address | Contents
    --------------------------
     f    |   &f    |  &struct
     ff   |   &ff   |  &f
     fff  |   &fff  |  &ff
    Here, f is a pointer (to a struct) whose contents is the address of a structure in memory. ff is a pointer (to a pointer to a struct) whose contents is the address of f. fff is a pointer (to a pointer to a pointer to a struct) whose contents is the address of ff.

    Basically:

    fff lets us (through indirection) access ff.
    ff lets us (through indirection) access f.
    f lets us (through indirection) access the struct.

    But yes, any piece of memory has 2 parameters: the address and the contents. Think of memory like a big string of numbered boxes -- the address tells us which box number to look at, the contents are what we put in the box. Sometimes, we use a prointer, which is basically a note telling us to look in a different box.

    A triple indirection is a lot like the game we've all played as a child, wher you have to go through the house (or city) finding notes, each which tells you where the next note is. In this case, you look on box &fff, and it says "look in box &ff". Box &ff says "look in box &f", and box &f says "look at box &struct". Inside box &struct is the struct you created.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    okey, thanks again. I got it. The whole confusen was because the syntax thing , e.g using the & (ampersand) to asign the address of the pointer. I though you didnt have to do that because a pointer is nothing but a address anyway. But then i would be asigning the address directly to the pointer right and not the address of the pointer. hmm, if i got that right , i got it.

    But i fully understand the logic here.

    Cheers!
    G'n'R

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    That's right.

    For example, f contains the address of the struct. ff is declared as containing "an address to a pointer containing the address of a struct".

    If you did force ff = f (which can be forced by casting, although it's completely wrong to do) ff would have the address of the struct, not the address of something with the address of a struct.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    55
    okey, got it. Cheers


    G'n'R

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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