1

I was playing through c++ and trying to understand vector and its signature . In below method printPrimes I need to use pointer with address of why ?

Is vector<int> &primes not enough as from main method printPrimes is already sending address .

void printPrimes(long long l, long long r, vector<int>* &primes) { // some code } vector<int>* sieve() { vector<int> *prime = new vector<int>(); return prime; } int main() { vector<int> *primes = sieve(); printPrimes(l, r, primes); return 0; } 
16
  • 1
    "In below method printPrimes I need to use pointer with address of why ?" The & means reference, not address of. Commented Mar 23, 2018 at 9:18
  • In C++ a & in the function parameters means reference and not address like in C. So you are giving a reference of a pointer to this function. A similar effect can be achieved by using a double pointer except that when you call the function you would need to use & to get the address of the pointer. Commented Mar 23, 2018 at 9:19
  • why i need to use reference and * together Commented Mar 23, 2018 at 9:19
  • 2
    The declaration vector<int>* &primes declares primes to be a reference to a pointer to a vector of int elements. You might need it if you want to change where the pointer is pointing. It's unlikely you need to do that though, so stop using pointers at all. Commented Mar 23, 2018 at 9:19
  • 1
    @MSalters The statement is that in C++ a & can also mean reference, while in C when you do use a & it is only used to get the address of something. Commented Mar 23, 2018 at 9:30

3 Answers 3

4

I need to use pointer with address of

Here, & does not mean "address of"; it means the type "reference to".

It's clearer if you write it not like this:

vector<int>* &primes 

but like this:

vector<int>*& primes 

Though the choice of whitespace is artificial, that better documents that this & is "part of the type".

Have some types:

  • std::vector<T> = A vector of Ts
  • std::vector<T>& = A reference to a vector of Ts
  • std::vector<T>* = A pointer to a vector of Ts
  • std::vector<T>*& = A reference to a pointer to a vector of Ts
  • std::vector<T>*** = A pointer to a pointer to a pointer to a vector of Ts
  • std::vector<T>**& = A reference to a pointer to a pointer to a vector of Ts

…and so forth.

As for why you need a vector<int>*& for printPrimes to do its job, we could not tell you without actually being able to see it. I will say that it seems unlikely it needs a pointer at all, and that if it wants to modify that pointer it's going to cause problems with the new and delete in the calling scope.

In fact, all that dynamic allocation is completely pointless and only complicates things.

The following was likely intended instead:

void printPrimes(long long l, long long r, vector<int>& primes) { // some code } vector<int> sieve() { vector<int> prime; return prime; } int main() { vector<int> primes = sieve(); printPrimes(l, r, primes); } 
Sign up to request clarification or add additional context in comments.

Comments

1

vector<int>* &primes parameter has to be read this way:

Reference to a pointer of vector of int

and not

Address of a pointer of vector of int (which, you are right, would be useless)

Passing by reference allows to directly manipulate any instance outside of scope (like with pointers, but a safer way since a reference cannot be nullptr, and its existence is auto-managed (no need to delete)).

Comments

1

In c++ & in function parameter used to pass parameter by reference. vector<int>* &primes declares primes to be a reference to a pointer to vector<int>.

If printPrimes means to print only the vector passed to the function then the signature

void printPrimes(long long l, long long r, vector<int> &primes); 

can also do the job.

Reference to a pointer is needed when the pointer passed to the function is need to be modified and it's effect is expected to seen in the caller function.

void foo(int*& p){ p = new int[10]; // rest of the code } 

if a function bar is calling foo like

void bar(/* some parameters */){ // ... int *p; foo(p); // rest of the code } 

foo is modifying the pointer itself and this modification will be seen to bar also and memory allocated to p can be accessed from bar.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.