Solved

Pointers in C++

Posted on 2011-03-01
6
208 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
>>  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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>  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
Comment Utility
//
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 6

Expert Comment

by:Mistralol
Comment Utility

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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

762 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

13 Experts available now in Live!

Get 1:1 Help Now