A question regarding the duality of pointers and arrays and another on structures.

S

Squignibbler

Hi all,

I have a question regarding the C++ programming language
regarding the nature of the relationship between pointers and arrays.

If the statement MyArray[x] is functionally identical to
*(MyArray+x), what statement is functionally identical to
MyArray[2,x]?

I ask this question because when I create a dynamic array with one
dynamic dimension e.g p_MyArray = new int [2,x] how do I access the
individual elements of the array using the pointer p_MyArray?


On a similar note, If I have created a dynamicly sized array of
structures, how do I retrieve the address of the member variable in any
given structure in the array?

I am aware that X = p_StructreArray->Member_variable will
retrive the value of MemberVariable through pointer p_StructureArray,
but how do I get the address?!

Thanks for any help or insight you can offer a beffuddled mind!
 
P

Phlip

Squignibbler said:
I have a question regarding the C++ programming language
regarding the nature of the relationship between pointers and arrays.

If the statement MyArray[x] is functionally identical to
*(MyArray+x), what statement is functionally identical to
MyArray[2,x]?

Allow me to be the first of many.

The expression 2,x returns x. The 2 is thrown away. So you have a flat
array, not a two-dimensional array.

Get that with MyArray[2][x]. Now the array aggregate is recursively defined.

And pointers and arrays are not themselves dual. Accessing them with [] is.
On a similar note, If I have created a dynamicly sized array of
structures, how do I retrieve the address of the member variable in any
given structure in the array?

I am aware that X = p_StructreArray->Member_variable will
retrive the value of MemberVariable through pointer p_StructureArray,
but how do I get the address?!

&p_StructreArray[x].Member_variable

The [] and . precede the &, so it evaluates last, and returns the address of
one of the Member_variables.
 
S

Squignibbler

Thankyou very much indeed for your accurate, and speedy advice. You
have helped greatly in my understanding of the situation. Thanks again,


SquigNibbler
 
S

Squignibbler

Please I must ask for some further help:

Umm....okay now im really confused!! if I use

cout << &p_StructreArray[x].Member_var­iable I see an address as
one would expect...

however if I assign a pointer the value and print that i get the value
of the member variable!!

char *p_AVariable;
.........
........

p_AVariable = &p_StructreArray[x].Member_variable;
cout << p_AVariable; //this display the meber variable not the
address...why?!

IF anyone could shed an light on my ignorance i would be very gratefull!
 
P

Phlip

Squignibbler said:
cout << &p_StructreArray[x].Member_var­iable I see an address as
one would expect...

What type is Member_variable?

<< depends on one overloaded operator<< for each type that can stream. If
the input isn't char*, the operator that streams a pointer's address will
compile in.

When you assign to a char * and use that, you might be shifting the type to
one that << interprets as a string.
 
S

Squignibbler

Quote: Philip wrote..
"What type is Member_variable? "

It is a array of 30 chatacters, used to hold a string read from a file.


Quote: Philip wrote..
"When you assign to a char * and use that, you might be shifting the
type to
one that << interprets as a string."

Thus it makes alot of sense, although the string is appearing when try
and pass the address aquired by the
"&p_StructreArray[x].Member_variable" statement to a variable or use it
as the return value of a function. Please could you suggest away round
this problem, so i can store the address's retrieved using the
"&p_StructreArray[x].Member_var­iable" method?

Thanks for your help, your helping more than you can imagine. :D :D
:D
 
M

Mike Wahler

Hi all,

I have a question regarding the C++ programming language
regarding the nature of the relationship between pointers and arrays.

If the statement MyArray[x] is functionally identical to
*(MyArray+x), what statement is functionally identical to
MyArray[2,x]?

*(MyArray + x)
Read about the comma operator.
I ask this question because when I create a dynamic array with one
dynamic dimension e.g p_MyArray = new int [2,x]

That array has a single 'dimension', that is, 'x'.
See the FAQ for how to allocate 'multidimensional'
arrays with 'new':
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16

However, I'd recommend using containers instead (e.g.
how do I access the
individual elements of the array using the pointer p_MyArray?


On a similar note, If I have created a dynamicly sized array of
structures, how do I retrieve the address of the member variable in any
given structure in the array?

I am aware that X = p_StructreArray->Member_variable will
retrive the value of MemberVariable through pointer p_StructureArray,
but how do I get the address?!

Same way you get the address of any object, use the address operator.
&p_StructreArray->Member_variable
Thanks for any help or insight you can offer a beffuddled mind!

Which C++ book(s) are you reading that don't explain these
issues?

-Mike
 
S

Squignibbler

C++ from the ground up.. .. its an older book from 1994... it does talk
about the & operator, but I have just had no success with usign it in
this one program i wrote!

int * X;

X = &p_StructreArray->Member_variable; //is a char[30] array

cout << X; //this displays the word in Member_variable :-(

cout << &p_StructreArray->Member_variable; //this displays the

address :)

now philip mentioned the fact that my compiler may be performing a
substitution because X is a pointer to an array of characters...
OR is there any other valid reason why this may occur!?

Quote "However, I'd recommend using containers instead (e.g.
std::vector<std::vector>. Then all the memory management
is handled for you automatically."

Thanks!! This might be just what i ned to learn next. I must say it is
amazing that people are prepared to take time to help others :D
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top