Thread: more help on double pointers

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    51

    more help on double pointers

    Ok, I read the previous posts on here, but things are a little blurry. I have no problem understanding single pointers and the similarities between them and arrays, but just get lost with doubles.

    For example, I need to keep a list of strings and use dynamic allocation with it. I declare:

    >>> char **thingies;

    Now, if I allocate memory for a 32 char string to go in the 5 pointers spot:

    >>> thingies[5] = malloc(32 * sizeof(char));

    And store the string there:

    >>> strcpy(thingies[5], "ajhdflkahsdkfhasasdf");

    It seems like this would store the address and the string in the same place. Could someone plz explain if I have done this right or wrong and why.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    hmm

    hey
    from what i can see..
    firstly your not allocating space for the array of strings..

    i.e.

    char **thingies

    this is a pointer to a pointer to a char (which could be an array)

    thingies[0] could point to a char
    (that u allocate space for)
    - with no problems

    thingies[1]
    HASNT had space allocated for it so it could overwrite important memory

    u need to allocate memory for the array first.. and then allocate the memory that each array part points to.

    i hope thats clear enough.

    cheers,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    Fayetteville Rocks!

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    After a few minutes, it's starting to sink in (and my code started working). I still get a little lost if i put too much thought into it, so I'll just accept the fact that it works that way. Thnx for the help.

  5. #5
    Sayeh
    Guest
    Look, you're making it overcomplex in your mind. Pointers are _very simple_. Indirection is _very simple_.

    I'm not sure you understand how multi-dimension arrays look in RAM, either...

    A single dimension array is an array of like-size elements.

    A double-dimension array is an array of pointers to arrays of like-size elements.

    A multi-dimension array is an array of pointers to an array of pointers to an array of.... and finally to an array of like-size elements.

    Creating a variable like this:

    Code:
    char **myVar;
    DOESN'T allocate more than 4 bytes of RAM. '**myVar' is a pointer. It's just that the '**' tells the compiler that it will be pointing to another pointer (an address), not an element (data), and then pointing through it to the actuall elements.

    People get confused with pointers because they confuse the syntax of the language (that grammar required to help the compiler correctly assemble the code), versus what they are actually trying to do in RAM.

    And truthfully, it is _much_ easier to simply use the [] or [][] syntax for array access. If you are writing performance code, you can always say:

    Code:
    char   myMultiArray[5][2][3];
    char   *myPtr;
    . . .
    
    myPtr = &myMultiArray[3][2][3];          /* gets address to element */
    ...then walk the elements with the pointer. It's much easier to read, too.

    ---

    Look at it this way: You have a house. Your neighbor has a house. His neighbor has a house. You don't know where his neighbor his, but you know that your neighbor keeps his neighbor's address written down in his house.

    So, in order to get to your neighbor's neighbor (**), you have to go _into_ your neighbor's house (*) and look at what's written down. You then walk to his neighbor's house (*), and you can see what's inside at that location, which is 'stuff'.

    Each house can either have 'stuff' in it, or a piece of paper with some other neighbor's address-- but not both.

    ---

    enjoy.

  6. #6
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    A pointer to a pointer can be used as just that or it can be used for the dynamic allocation of a 2d array or as a 1d array of pointers (when dynamically allocated).

  7. #7
    Sayeh
    Guest
    samGwilliam:

    > A pointer to a pointer can be used as just that or it
    > can be used for the dynamic allocation of a 2d array
    > or as a 1d array of pointers (when dynamically allocated).

    This is true, but _only_ if you are building and managing the array. Your compiler DOES NOT build multi-dimension arrays this way.

    If you make the compiler allocate the multi-dim array, and then you try to access it this way, it will break.

    It is important that you learn how your tools actually work.

    enjoy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  4. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM