Double pointer as a parameter of function

Hi Everyone,

I'm trying to find out how double pointers work. I have a strucure of fruits and a function which should change its parameters. Fruits are in a small array. I want to use double pointer as a parameter of function changeFruit to change fruit values. It gives me a mistake of invalid argument for the function...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct Fruit {
    int mellon;
    int banana;
    int green;
};


void changeFruit(Fruit **arry, int index, int color, int weight, int age) {

	(*arry[index]).mellon = color;
	(*arry[index]).banana = weight;
	(*arry[index]).green = age;
 }

int main() {

	Fruit arr[2];

	arr[0].mellon = 1;
	arr[0].banana = 2;
	arr[0].green = 3;

	arr[1].mellon = 11;
	arr[1].banana = 22;
	arr[1].green = 33;

	int indexx = 0;
	int colorr = 111;
	int weightt = 222;
	int agee = 333;

	changeFruit( &arr, indexx, colorr, weightt, agee);

  return 0;
}
Last edited on
Your function expects a pointer to a pointer to Fruit as the first argument, but you're trying to pass a pointer to an array of two Fruits. These aren't the same things, and aren't even convertible to each other, so you have a compile-time error.

Change your function declaration to:

void changeFruit(Fruit (*arry)[2], int index, int color, int weight, int age) {
Can I change the function or call function to work with these parameters? What I'm trying to do is that the function should take an array of pointers to Fruits and initializes the array at the given index to be a new Fruit with the given color, weight, and age values.


1
2
3
void changeFruit(Fruit **arry, int index, int color, int weight, int age) {

}
Last edited on
This isn't really a realistic use of double pointers - you could perform the same function with just a straight pointer. BUT, seeing as you're trying out double pointers, you could call your function like this ...

1
2
3
Fruit arr[2];
Fruit* arrPtr = arr; // shorthand for &(arr[0]);
changeFruit(&arrPtr, ...);


Jim
Thank you! Just one more question, is there any other way, how can I rewrite the function(maybe call as well) by it self with the same declaration just to take a random array of pointers to Fruits and initializes the array at the given index to be a new Fruit with the given values?
Last edited on
Oops, sorry - I just realised that my quick answer will only work for index == 0. Any other value will fail with memory errors as you'll be accessing unallocated memory.

You really need an array of Fruit* to pass to your changeFruit function.

1
2
3
4
5
Fruit arr[2];
Fruit arr2[2];

Fruit* arrPtrs[2] = { arr, arr2 }; // An array of Fruit* 
changeFruit(arrPtrs); // Shorthand for &(arrPtrs[0]), which is a Fruit** 


If you want the function to update the contents of the array, with a whole new Fruit instance ...

1
2
3
4
5
6
void changeFruit(Fruit** arry, int index, int color)
{
  arry[index] = new Fruit();
  arry[index]->color = color;
  etc
}


Jim
I just want to update value of struct Fruit so I do not create any new instance of Fruit.

Just one more and hopefully last question. How can I change value of
arr[1].red
I've triead it a few times and I'm able to approach only
arr[0].red
and
arr2[0].red

Thank you!
Last edited on
Hi, sorry for the delay - we had a long weekend here ;-)
Did you work this out?

What error are you getting? You should just be able to assign values with
1
2
3
Fruit arr[2];

arr[1].green = 5;


Jim
Topic archived. No new replies allowed.