How send pointer to function by reference? For example, I want to send it to one function:
int **example;
Thank you.
How send pointer to function by reference? For example, I want to send it to one function:
int **example;
Thank you.
Your question's confusing many people, as "send (pointer to function)" differs from "(send pointer) to (function)"... given your example variable, I'm assuming you want the latter...
The reference aspect is denoted last:
return_type function_name(int**& example) // pass int** by ref
And just in case an int*
is what you wanted to pass, and the **
thing in your example code was an attempt at passing that by reference - for int*
it should actually be:
return_type function_name(int*& example) // pass int* by ref
UPDATE
Your code:
void Input(float **&SparceMatrix1,int &Row1,int &Column1)
{
cin>>Row1; cin>>Column1;
*SparceMatrix1 = new float [Row1];
/*for(int i=0;i<Row1;i++) (*SparceMatrix1)[i]=new float [Column1];*/
}
The type of SparceMatrix1
(it's spelt "sparse" btw), means it can track data like this:
float** ---------> float* -----------> float
SparceMatrix1 *SparceMatrix1 **SparceMatrix1
So, you try to set *SparceMatrix1
to point at Row1
float
s, but SparceMatrix1
hasn't been pointed at anything yet, so you can't deference / follow it at all. Instead, you should do this:
if (cin >> Row1 >> Column1)
{
SparceMatrix1 = new float*[Row1];
for (int i = 0; i < Row1; ++i)
SparceMatrix1[i] = new float[Column1];
}
else
SparceMatrix1 = nullptr; // pre-C++11, use NULL, or throw...
As you can see, it's a little tricky to do all this stuff correctly, so you'd be better off using a std::vector<std::vector<float>>
instead (which is much easier to get right but already tricky enough - you'll find too many stackoverflow questions about them too).
When you have pass a int x
to a function foo()
and you receive it like,
foo(int& var), here `int&` for `reference to int`, just replace it with whatever reference you want to pass, in your case `foo(int** &)` .
^^^^
If you want to pass a char pointer
(char*) by reference just do foo(char* &)
.
My advise not only for this case but in general: when you have an issue with a complex type use typedef. It will not only help you to resolve this particular one but also to understand how it works better:
class Foobar;
typedef Foobar* FoobarPtr;
void function( FoobarPtr &ref );