views:

242

answers:

1

Not sure why I'm getting this error. I have the following:

int* arr = new int[25];

int* foo(){
   int* i;
   cout << "Enter an integer:";
   cin >> *i;
   return i;
}

void test(int** myInt){
   *myInt = foo();
}

This call here is where I get the error:

test(arr[0]);   //here i get invalid conversion from int to int**
+8  A: 

The way you've written it, test takes a pointer to a pointer to an int, but arr[0] is just an int.

However, in foo you are prompting for an int, but reading into a location that is the value of an uninitialized pointer. I'd have thought you want foo to read and return and int.

E.g.

int foo() {
   int i;
   cout << "Enter an integer:";
   cin >> i;
   return i;
}

In this case it would make sense for test to take a pointer to an int (i.e. void test(int* myInt)).

Then you could pass it a pointer to one of the int that you dynamically allocate.

test(&arr[0]);
Charles Bailey