-4

How send pointer to function by reference? For example, I want to send it to one function:

int **example;

Thank you.

n00dle
  • 5,949
  • 2
  • 35
  • 48
  • possible duplicate of [How do you pass a function as a parameter in C?](http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c) – MrHug Oct 13 '14 at 13:27

4 Answers4

0

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

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • void Input(float **&SparceMatrix1,int &Row1,int &Column1) { cin>>Row1; cin>>Column1; *SparceMatrix1=new float [Row1]; /*for(int i=0;i – Moein Salimi Oct 13 '14 at 13:40
0

Just declare it as such:

void f(int*&);
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

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

iampranabroy
  • 1,716
  • 1
  • 15
  • 11
0

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 );
Slava
  • 43,454
  • 1
  • 47
  • 90