Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Arrays and References

Reply
Thread Tools

Arrays and References

 
 
kieran@cyrocom.co.uk
Guest
Posts: n/a
 
      08-27-2005
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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-27-2005
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.

 
Reply With Quote
 
 
 
 
Varun Sud
Guest
Posts: n/a
 
      08-31-2005
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...cle_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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Backwards references in arrays of arrays of... Ohad Lutzky Ruby 0 10-14-2006 12:09 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57