Solved

Pointers in C++

Posted on 2011-03-01
6
209 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now