Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pointers in C++

Posted on 2011-03-01
6
Medium Priority
?
216 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

661 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