Jump to content
  • Advertisement
Sign in to follow this  
Jamiernmd

C++ Pointer Question

This topic is 2995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Probably a really stupid question but I'm trying to access an individual element of a vector like this:

vVector[nIndex] = GetNum();

The trouble is vVector is a pointer to a vector. How do I go about performing this action?

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jamiernmd
I've tried that, I get an illegal indirection error


Operator precedence...

(*vVector)[nIndex] = GetNum( );

What would require such code if I may be so bold?

EDIT: Or the way presented in the post below.
Further example of why your getting this error:

*vVector[nIndex] = ..
is being read by your compiler as
*(vVector[nIndex]) = ..

Which is clearly not what you want. The illegal indirection error stems from the fact the the operator '[n]' dereferences the pointer and you can't dereference a reference.

I presume you already knew that and just made a silly mistake :P

[Edited by - CodeCriminal on July 13, 2010 7:11:34 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeCriminal
Quote:
Original post by Jamiernmd
I've tried that, I get an illegal indirection error


Operator precedence...

(*vVector)[nIndex] = GetNum( );

What would require such code if I may be so bold?


Ah thats done it. Thanks

The reason I have this is because I have a number of vectors and I wanted a single function where I could pass in a pointer to a vector and it would work universally with all vectors. I also want to be able to swap out individual elements of the vector without navigating the whole thing. (Btw I changed the name of the vector and function for simplicity). Why do you ask? Is there a better way of doing it?

Thanks for the responses people!

Share this post


Link to post
Share on other sites
Quote:
Original post by Jamiernmd
Quote:
Original post by CodeCriminal
Quote:
Original post by Jamiernmd
I've tried that, I get an illegal indirection error


Operator precedence...

(*vVector)[nIndex] = GetNum( );

What would require such code if I may be so bold?


Ah thats done it. Thanks

The reason I have this is because I have a number of vectors and I wanted a single function where I could pass in a pointer to a vector and it would work universally with all vectors. I also want to be able to swap out individual elements of the vector without navigating the whole thing. (Btw I changed the name of the vector and function for simplicity). Why do you ask? Is there a better way of doing it?

Thanks for the responses people!


I was just curious, without seeing the rest of your code its hard to determine whether or not a simple reference would have been more appropriate. If you had used a reference this problem would not have cropped up in the first place.

Example:
// Simplified version of what I THINK you may be doing.
void function(std::vector<int>* v)
{
for (unsigned int i = 0; i < v.size( ); ++i)
(*v) = getnum( );
}

// What I am recommending, use a reference.
void function(std::vector<int>& v)
{
for (unsigned int i = 0; i < v.size( ); ++i)
v = getnum( );
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!