Thread: Arrays of Pointers, how do they work?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  2. #2
    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?

  3. #3
    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!

  4. #4
    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, 02: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, 10:45 PM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM