Arrays of Pointers, how do they work?

This is a discussion on Arrays of Pointers, how do they work? within the C Programming forums, part of the General Programming Boards category; In my book, I'm given the example of Code: const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Arrays of Pointers, how do they work?

    In my book, I'm given the example of
    Code:
    const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

    In the previous sections it was made pretty clear that the memory address for an array is reserved before hand and not dynamic. Here we have a pointer array, that points towards other arrays whose size seems to be dynamic. How is this?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by yougene View Post
    In my book, I'm given the example of
    Code:
    const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

    In the previous sections it was made pretty clear that the memory address for an array is reserved before hand and not dynamic. Here we have a pointer array, that points towards other arrays whose size seems to be dynamic. How is this?
    They are not dynamic, they just have different sizes. At any rate, an array of pointers which all point to dynamically allocated things is not a problem either.

  3. #3
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    for a start theyre not dynamic in anyway because the sizes are automatically set during declaration.

    as i read this, it means declare an array of character pointers that are given the values hearts, diamonds, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so your array is defined size: 4.
    The content pointed to by the four elements in the array is strings that vary in lenght. But the strings are just string constants, not arrays.

    It's the same as
    Code:
    char *p = "Something";
    p is a pointer. It points to the string "Something", which the compiler will store somewhere in the data section of your executable [modern systems would put it in a "read-only" data section, so you can't change it]. This is the same with any other string constant, for example printf("Hello, World\n") would obviously store the string "Hello, World!\n" somewhere in the executable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahh, so the amount of letters in the string determines the fixed size of the array.

    Sorry it took so long to respond. I get a little overwhelmed with school for a little while.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Sort of. It's best not to think of it as an array, because "Hearts" takes up less space than "Diamonds", which goes against how arrays work.

    You basically have two things to think about:
    • An array of four pointers, which point to strings.
    • Four read-only strings stored elsewhere, which the pointers point to.

    These strings each take up exactly as much space as they need.

    Sorry if I'm just re-stating what everyone else has already said.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    But each string can be thought of as an array of characters. So the size of each individual character array is determined by the string value assigned to it, no?

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Quote Originally Posted by dwks View Post
    Sort of. It's best not to think of it as an array, because "Hearts" takes up less space than "Diamonds", which goes against how arrays work.

    You basically have two things to think about:
    • An array of four pointers, which point to strings.
    • Four read-only strings stored elsewhere, which the pointers point to.

    These strings each take up exactly as much space as they need.

    Sorry if I'm just re-stating what everyone else has already said.

    Great explanation. Helped me as well!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Great explanation. Helped me as well!
    Glad to hear it . . .

    But each string can be thought of as an array of characters. So the size of each individual character array is determined by the string value assigned to it, no?
    That's right. I was just saying that thinking of this data structure as a 2D array is confusing. You could think of those "four read-only strings" that I mentioned as arrays if it's easier for you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Crystal clear, thank you

  11. #11
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    Quote Originally Posted by dwks View Post
    Sort of. It's best not to think of it as an array, because "Hearts" takes up less space than "Diamonds", which goes against how arrays work.
    You basically have two things to think about:
    • An array of four pointers, which point to strings.
    • Four read-only strings stored elsewhere, which the pointers point to.

    These strings each take up exactly as much space as they need.
    Sorry if I'm just re-stating what everyone else has already said.
    No, that's fine. I think you worded it best right there. And I would like to clarify again for simplicity reasons that those strings are not stored in the pointers, just the address where the string is stored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 03:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 11:45 PM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 02:41 AM

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