Arrays and References

K

kieran

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
 
R

Rolf Magnus

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.
 
V

Varun Sud

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top