Solved

Pointers in C++

Posted on 2011-03-01
6
212 Views
Last Modified: 2012-05-11
Hello experts.

void aaa(bbb* aVar)
{
      bbb myVar;
      
      myVar = aVar[1]; //compiles
      //myVar = *aVar[1]; //does not compile

}

This is my thinking process:

a. bbb * aVar

aVar is a pointer to address of bbb.

We can treat aVar as an array where
aVar[0] points to first address of bbb
aVar[1] points to second address of bbb, and so on.

So...

bbb = aVar[1] should make bbb equal to address of second
element of aVar

bbb = *aVar[1] should make bbb equal to value of second
element of aVar


Where do I get it wrong?

Thank you

panJames


0
Comment
Question by:panJames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35006367
>>  aVar is a pointer to address of bbb.

aVar is a pointer to the location of an object of type bbb. The indexing operation ( [n] ) is equivalent to saying

    *(somePtr + n)

What you are trying to do with this line:

    myVar = *aVar[1];

is dereference whatever is found in the first slot of the array aVar.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35006395
>>  dereference whatever is found in the first slot of the array aVar.

Using the aforementioned equivalence, what you attempting to do is:

    **(aVar + 1)
0
 

Author Comment

by:panJames
ID: 35006411
//
What you are trying to do with this line:

    myVar = *aVar[1];

is dereference whatever is found in the first slot of the array aVar.
//


why is it illegal?

panJames
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mistralol
ID: 35006934

Thats kinda of true but what you are really doing is this

**(avar + sizeof(bbb));


void aaa(bbb* aVar)
{
      bbb myVar;
     
      myVar = aVar[1]; //compiles
      //myVar = *aVar[1]; //does not compile

}

The first works because myVar is taking a copy of the data in aVar[1] from the array slot 1.
So its the same as memcpy(&myVar, &aVar[1], sizeof(myVar));







0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007123
My C-Fu is not that strong, but IIRC, unless aVar is a pointer to an array of pointers, array of arrays, or a pointer to a pointer, you will get an error.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007128
*with the above syntax  = )
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question