Public Group

# C++ Pointer Question

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

## 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 on other sites
*vVector[nIndex] = GetNum();

##### Share on other sites
I've tried that, I get an illegal indirection error

##### Share on other sites
Quote:
 Original post by JamiernmdI'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] = ..
*(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 on other sites
Or do like this:
vVector->operator[](nIndex) = GetNum();

##### Share on other sites
Quote:
Original post by CodeCriminal
Quote:
 Original post by JamiernmdI'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 on other sites
Quote:
Original post by Jamiernmd
Quote:
Original post by CodeCriminal
Quote:
 Original post by JamiernmdI'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 on other sites
Will try that. Thanks for your help!

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631367
• Total Posts
2999594
×