Sumeet Kumar Yadav
Sumeet Kumar Yadav

Reputation: 12975

Use of * and & together in function signature

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;
}

Upvotes: 1

Views: 1502

Answers (3)

haccks
haccks

Reputation: 106012

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.

Upvotes: 1

Benjamin Barrois
Benjamin Barrois

Reputation: 2686

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)).

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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);
}

Upvotes: 4

Related Questions