Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
What function argument i have to pass to function Myfunk,c without changing the defition?

void MyFunc(int * & data) {data++;}
void main () {
 int a = 5;
 MyFunc(&a);
 cout << a;
}


What I have tried:

I have tried the address memory of the &a.
Posted
Updated 2-Feb-17 7:01am

It takes a reference to a pointer (to int).
Try, for instance:
C++
#include <iostream>
using namespace std;

void MyFunc(int * & data) {data++;}

int main ()
{

  int a [] = { 12, 15, 27, 42 };

  int * p = &a[2];

  cout << *p << endl;

  MyFunc( p );

  cout << *p << endl;
}
 
Share this answer
 
v2
You have to pass a reference to an int*:
C++
int someVal = 123;
int *pSomeVal = &someVal;
MyFunc(pSomeVal);
Because a reference must be an lvalue, you must create a variable rather than just passing the address of a variable which is not an lvalue.

Because your function is incrementing the pointer, using an array makes more sense (avoid out-of-bounds access):
C++
int someVals[] = { 1, 2, 3 };
int *pSomeVal = someVals;
MyFunc(pSomeVal);
cout << *pSomeVal;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900