446,234 Members | 1,896 Online
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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! Jul 23 '05 #1
7 Replies

 P: n/a Squignibbler wrote: 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. -- Phlip http://industrialxp.org/community/bi...UserInterfaces Jul 23 '05 #2

 P: n/a Thankyou very much indeed for your accurate, and speedy advice. You have helped greatly in my understanding of the situation. Thanks again, SquigNibbler Jul 23 '05 #3

 P: n/a 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! Jul 23 '05 #4

 P: n/a Squignibbler wrote: 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. -- Phlip http://industrialxp.org/community/bi...UserInterfaces Jul 23 '05 #5

 P: n/a 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 Jul 23 '05 #6

 P: n/a wrote in message news:11**********************@l41g2000cwc.googlegr oups.com... 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-lit...html#faq-16.16 However, I'd recommend using containers instead (e.g. std::vector. Then all the memory management is handled for you automatically. 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 Jul 23 '05 #7

 P: n/a 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. 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 Jul 23 '05 #8