Solved

Pointers in C++

Posted on 2011-03-01
6
210 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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