Hello everyone.
I have to write an example in which you use a function having as argument a pointer passed by reference in C++.
Can you provide an example like this:
funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "funz.h"
#include <iostream>
usingnamespace std;
int main()
{
int b = 10;
cout << "Before of funz(), b = " << b << endl;
funz(?); // (2)
cout << "After of funz(), b = " << b << endl;
return 0;
}
#include <iostream>
void funz( int*& a )
{
staticint b = 45 ;
a = &b ;
}
int main()
{
int i = 777 ;
int* pi = &i ;
std::cout << "int at address " << pi << " has value " << *pi << '\n' ;
funz(pi) ;
std::cout << "int at address " << pi << " has value " << *pi << '\n' ;
}
void funz( int *&a ); wat? *&a is gibberish, I wouldn't be surprised if it failed to compile.
Um, no. It's exactly what the OP said it was - a reference to an int*. A pointer is a data type like any other, and you can have references to them like any other type.
To the OP, an example might be a function which allocates some memory, and returns the pointer to that newly-allocated memory as an argument, by reference.
That's nice, but it doesn't demonstrate that you're passing the pointer by reference. To demonstrate that, you want your function to change the value of the actual pointer, b, itself - not the value stored in the memory it's pointing to.