Arrays and References

Discussion in 'C++' started by kieran@cyrocom.co.uk, Aug 27, 2005.

  1. Guest

    Hi,
    I'm sorry for this question, as it probably has a really simple answer,
    but say I have the following code:

    std::string* strs[12];

    Does this create a pointer to an array of strings, or does it create a
    2 dimentional array of strings, or does it create an array of 12 string
    pointers?
    Thanks for your help,
    Cheers,
    Kieran
     
    , Aug 27, 2005
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > std::string* strs[12];
    >
    > Does this create a pointer to an array of strings,


    No.

    > or does it create a 2 dimentional array of strings,


    No.

    > or does it create an array of 12 string pointers?


    Yes.
     
    Rolf Magnus, Aug 27, 2005
    #2
    1. Advertising

  3. Varun Sud Guest

    Hi, this is a very easy question but very often asked by beginners. The
    point here is you should know how to decrypt pointer declarations.
    Please note the examples may not be very readable if you do not use a
    fixed width font to read. These and many other topics related to
    pointers are explaine din detail in an article put up on my website:

    Article link:
    http://www.geocities.com/varunhostel/TechnicalArticles/PointerArticle/PointerArticle_Intro.html


    The steps normally followed to decrypt a pointer declaration are:
    1. Start with the name that will identify the pointer, which is known
    as the identifier.

    2. Move to the right until you encounter a right-parantheses [symbol =
    ")" ] or reach the end. Do not stop if the () brackets are used to pass
    parameters to a function. Also do not stop on encountering brackets
    used with arrays: [ ].

    3. Now go left of the identifier to continue deciphering the
    declaration. Keep going left until you find a left-parantheses [the
    symbol "(" ] or reach the end. Do not stop if the brackets are used to
    pass parameters to a function.

    4. The whole interpretation should be a nice long sentence.

    This will not be clear without a couple of examples. So here they are:

    1. int *ptr[5]

    ptr is an array of size 5... [nothing more on the right]
    ....of pointers to an int [nothing more to read]

    2. int (*ptr)[5]

    ptr is... [ the ) indicates: "stop reading further to the right".
    Now read left.]
    ....a pointer to... [we encounter a (. Now read further right.]
    ....an array of size 5... [nothing more on right]
    ....of type int. [nothing more on left]

    3. ( * f1 ( ) ) ( )
    1 2 3 4 5 6

    f1 is a function... [we see a ), character 5. Now read left.]
    [The arguments in () at position 4 should be read as one character. Do
    not stop at () that pass parameters to a function.]
    ....returning a pointer... [we see a (, character 1. Now read right.]
    ....to a function... [nothing more on right]
    ....returning an int [not specified, but implicit]


    Thanks,
    Varun Sud
    http://www.geocities.com/varunhostel/
     
    Varun Sud, Aug 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    459
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    470
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    626
    DanielEKFA
    May 16, 2005
  4. Philipp
    Replies:
    21
    Views:
    1,157
    Philipp
    Jan 20, 2009
  5. Ohad Lutzky
    Replies:
    0
    Views:
    116
    Ohad Lutzky
    Oct 14, 2006
Loading...

Share This Page