C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dereferencing problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alan Brown
Guest





PostPosted: Mon Mar 28, 2005 12:01 pm    Post subject: Dereferencing problem Reply with quote



Newbie question - Here are parts of a program I am working on.

class cData
{
public:
void ShowData();
void SetData(int i);
private:
int Price;
};
.....

// Set a pointer pData of type cData to an array
// of 10 objects of cData type on the heap
cData * pData = new cData[10];
.....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

Alan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Mon Mar 28, 2005 5:23 pm    Post subject: Re: Dereferencing problem Reply with quote



Alan Brown <rigelau (AT) yahoo (DOT) nospam.com.au> writes:

Quote:
cData * pData = new cData[10];
....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

In the expression pData[i]->ShowData(), you actually attempt to
dereference pData twice.

The expression pData[i] is equivalent to *(pData+i) (i.e. to
dereference a pointer to the ith element in the array that pData
points to). It's type is thus cData, and the operator to be used to
access the members of the cData object is ., not ->.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Peter MacMillan
Guest





PostPosted: Mon Mar 28, 2005 5:28 pm    Post subject: Re: Dereferencing problem Reply with quote



Alan Brown wrote:
Quote:
cData * pData = new cData[10];
....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

Because your structure is an array of cData, not an array of cData *.

Remember that pData[x] is equivilant to (*(pData + x)) - pData is a
pointer to the first (0th) element of the array.

Observe the two arrays in this example:

//---
#include <iostream>

using std::cout;
using std::endl;

struct MyStruct { int blah; };

int main(int, char **) {

// an array of MyStruct
MyStruct * arr1 = new MyStruct[5];

// an array of pointers to MyStruct
MyStruct ** arr2 = new MyStruct*[5];

for (int i = 0; i < 5; ++i) {
// you don't have to instantiate each element
// because it was created with the new MyStruct[5]
arr1[i].blah = i;

// you have to instantiate each element
// because the new MyStruct*[5] created a
// new array of empty pointers
arr2[i] = new MyStruct;
arr2[i]->blah = i;
}


for (int i = 0; i < 5; ++i) {
cout << arr1[i].blah << ":" << arr2[i]->blah << endl;
}


// these two lines are equivlant.
cout << arr1[2].blah << endl;
cout << (*(arr1 + 2)).blah << endl;


return 0;
}
//---

Quote:
Alan



--
Peter MacMillan
e-mail/msn: [email]peter (AT) writeopen (DOT) com[/email]
icq: 1-874-927

GCS/IT/L d-(-)>-pu s()Sad-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
M Jared Finder
Guest





PostPosted: Mon Mar 28, 2005 5:30 pm    Post subject: Re: Dereferencing problem Reply with quote

Alan Brown wrote:
Quote:
Newbie question - Here are parts of a program I am working on.

class cData
{
public:
void ShowData();
void SetData(int i);
private:
int Price;
};
.....

// Set a pointer pData of type cData to an array
// of 10 objects of cData type on the heap
cData * pData = new cData[10];
.....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

What you allocated with new cData[10] is an array of 10 cData's, and you
are returned a pointer to the first element in the array. Just like how
you do not have to use -> when you access a stack allocated array, like
if you did:

cData data[10];
data[i].ShowData();

you do not have to use -> if the array is allocated on the heap, with new.

-- MJF

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Mon Mar 28, 2005 5:30 pm    Post subject: Re: Dereferencing problem Reply with quote

Alan Brown wrote:
[snip]
Quote:
cData * pData = new cData[10];
....

Here, you're allocating 10 elements, and getting one pointer.

Quote:

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

You are correct that '->' is required to access members through
pointers. However, pData[i] is not a pointer; pData is the pointer
and pData[i] is equivalent to *(pData+i), i.e.: a dereference of the
pointer.

--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Mar 28, 2005 5:31 pm    Post subject: Re: Dereferencing problem Reply with quote

In article <Xns96279B6AE8EA0416c616e (AT) 211 (DOT) 29.133.50>, Alan Brown
<rigelau (AT) yahoo (DOT) nospam.com.au> writes
Quote:
Newbie question - Here are parts of a program I am working on.

class cData
{
public:
void ShowData();
void SetData(int i);
private:
int Price;
};
....

// Set a pointer pData of type cData to an array
// of 10 objects of cData type on the heap
cData * pData = new cData[10];
....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".

Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

But the index operator ([]) implicitly dereferences a pointer. So in the
above code pData is a pointer but pData[i] is the ith object in a
sequence pointed to by pData

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mark Van Peteghem
Guest





PostPosted: Mon Mar 28, 2005 5:32 pm    Post subject: Re: Dereferencing problem Reply with quote

Alan Brown wrote:

Quote:
Newbie question - Here are parts of a program I am working on.

class cData
{
public:
void ShowData();
void SetData(int i);
private:
int Price;
};
....

// Set a pointer pData of type cData to an array
// of 10 objects of cData type on the heap
cData * pData = new cData[10];
....

pData[i].Showdata(); // This works OK

When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of ->".


pData is a pointer to an object of type cData.

However, pData[i] is a /reference/ to an object of type cData, not a
pointer.
pData[i] is actually the same as (*(pData+i)).

--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Norbert Müller
Guest





PostPosted: Mon Mar 28, 2005 11:55 pm    Post subject: Re: Dereferencing problem Reply with quote

Alan Brown wrote:

Quote:
Newbie question - Here are parts of a program I am working on.

class cData
{
public:
void ShowData();
void SetData(int i);
private:
int Price;
};
....

// Set a pointer pData of type cData to an array
// of 10 objects of cData type on the heap
cData * pData = new cData[10];
....
Think about what you are doing here:

You
- allocate space for an array of cData object
- declare a pointer to a cData object
- initialize the pointer with the address of the first element in the array
So pData is not a pointer to an array but a pointer to the first element.
Another interpretation of pData is simply the name of the array.

Quote:

pData[i].Showdata(); // This works OK


That is fine (assuming i is an integer between 0 and 9):
You call the Showdata method of the ith object
Quote:
When I try

pData[i]->ShowData();

I get the error message "Pointer to structure required on left side of
->".

On the left side of -> is not a pointer but an object.
Why?? I thought -> was required when dereferencing a pointer and . was
used to access a member of an object using the object name.

Alan

If you want to use pointers you could do something like this:

cData * secondPointer = pData;
for (int i = 0; i < 10 ; ++i) {
secondPointer->ShowData();
++secondPointer;
}

Maybe you should have a closer look to your favorite C++ textbook.

HTH
Norbert


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.