473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_variabl e will
retrive the value of MemberVariable through pointer p_StructureArra y,
but how do I get the address?!

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

Jul 23 '05 #1
7 2449
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_variabl e will
retrive the value of MemberVariable through pointer p_StructureArra y,
but how do I get the address?!


&p_StructreArra y[x].Member_variabl e

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

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #2
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
Please I must ask for some further help:

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

cout << &p_StructreArra y[x].Member_var*iab le 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_StructreArra y[x].Member_variabl e;
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
Squignibbler wrote:
cout << &p_StructreArra y[x].Member_var*iab le 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
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_StructreArr ay[x].Member_variabl e" 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_StructreArr ay[x].Member_var*iab le" method?

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

Jul 23 '05 #6
<Sq**********@h otmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.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 'multidimension al'
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<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_variabl e will
retrive the value of MemberVariable through pointer p_StructureArra y,
but how do I get the address?!
Same way you get the address of any object, use the address operator.
&p_StructreArra y->Member_variabl e

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
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_StructreArra y->Member_variabl e; //is a char[30] array

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

cout << &p_StructreArra y->Member_variabl e; //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

Jul 23 '05 #8

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

Similar topics

94
4683
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++. So, are there any examples (without pointers, references, or adress-taking), which would have a different result in Python and in C++? I would appreciate all insights or pointers to literature. TIA,
4
2224
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and in a readable format. This is needed to provide manually constructed test input to a function, which works with this tree. Number of types is huge, and I want to avoid hand-coding read/write functions for each type. Ideally, I'm thinking about...
8
3672
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
7
2474
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop processing the array. typedef struct screen_disp { int sd_row; int sd_col; char *sd_buff; } SCR_DISP;
9
2420
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
5
3122
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
9
2426
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
77
3995
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that goto sometimes makes program some more cleaner and easy to understand and also quite useful (in error handling cases). So why goto is outlawed from civilized c programmers community. is there any technical inefficiency in that.
62
4997
by: onkar | last post by:
Why use pointers at all??
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.