473,411 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Arrays and References

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 '05 #1
2 1095
ki****@cyrocom.co.uk 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.

Aug 27 '05 #2
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/

Aug 31 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
8
by: Greg | last post by:
In VB6 I made heavy use of control arrays I see they have been 'deprecated' in vb.Net, with a questionable explanation that they are no longer necessary which just addresses the event issue!...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
3
by: alcabo | last post by:
Hello, I'd like to improve several critical routines involving arrays (vectors and matrices)... How are arrays stored in memory? Row major or column major? (Like in C or like Fortran?)
13
by: sonjaa | last post by:
Hi I'm new to programming in python and I hope that this is the problem. I've created a cellular automata program in python with the numpy array extensions. After each cycle/iteration the...
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
10
by: John | last post by:
I have two separate arrays A and B. The comparison function only depends on elements in A (keys are in A), but the swap() function needs to swap not only A,A, but also B,B ; whenever it swaps A,A....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.