-3

Could someone please explain me what I'm doing wrong?

Actually I want getPointer() to return an array (with size of 3 pointers) of pointers to integers. That's my main plan. An alternative for me was to return a pointer to that array of pointers.

int value = 5;
int *(*array1)[3];


int* (*getPointer())[3] {
  int *pValue= &value; 
  array1[0] = pValue; // throws the error
  return array1;
}

int main() {
  return 0;
}

That's the error message:

error: incompatible types in assignment of 'int*' to 'int* [3]'

alve89
  • 971
  • 1
  • 9
  • 31
  • 2
    What are you actually trying to do? what are you trying to solve with array of 3 `int*` ? – Tony Tannous Jul 20 '20 at 18:52
  • @alve89 This int *(*array1)[3]; is not a declaration of an array. It is a declaration of a null-pointer to an array of three pointers. – Vlad from Moscow Jul 20 '20 at 18:55
  • Long story short: Due to an unexplainable linking error (undefined reference to....) I'm trying to "re-learn" the basics because I'm an absolute newbie. That's a very basic example for using pointers and arrays. The actual array is not of type `int*` but of type pointer to user defined type. – alve89 Jul 20 '20 at 18:55
  • @VladfromMoscow What am I supposed to do then to correctly declare an array of pointers? And could you please explain me "a declaration of a null-pointer to an array of three pointers"? – alve89 Jul 20 '20 at 18:56
  • @alve89 this should be of use: https://stackoverflow.com/q/388242/2079303 – eerorika Jul 20 '20 at 18:57
  • @alve89 `int *foo[42]`. But you can't return a plain old array of anything by value in C++. – Swordfish Jul 20 '20 at 18:57
  • @alve89 Just write int * array1[3]; – Vlad from Moscow Jul 20 '20 at 18:57
  • 1
    `std::array` can be returned, though. – sweenish Jul 20 '20 at 18:57
  • @VladfromMoscow Sure, that's quite obvious, my mistake... But how can I return a pointer to that array by function? So what was the correct function declaration? – alve89 Jul 20 '20 at 19:00
  • The array would just be used as an output parameter, typically. – sweenish Jul 20 '20 at 19:01
  • @alve89 If you want to return the array then the return type of the function should be int **. That is an array designator used in expressions including using them in the return statement is converted to pointer to its first element. – Vlad from Moscow Jul 20 '20 at 19:02
  • 1
    @alve89 Another approach is declare the return type as a referenced type to the array type. – Vlad from Moscow Jul 20 '20 at 19:05

1 Answers1

1

I solved it by myself by adding an & to the return and changed the array declaration to int *array1[3]:

int value = 5;
int *array1[3];


int *(*getPointer())[3] {
  //int *pValue= &value; 
  array1[0] = &value;
  return &array1;
}
alve89
  • 971
  • 1
  • 9
  • 31
  • Why are you declaring an argument you don't use in the function? Do you intent to use it in some code that isn't shown? Also, generally it's not a good idea for functions to change globals without a compelling reason. – doug Jul 20 '20 at 21:40
  • @doug: The problem is that I got a very weird linking error (undefined reference) while trying to directly access a global array. As a workaround I thought of a "handler function". – alve89 Jul 22 '20 at 11:19
  • Your function returns a ptr to an array of ptrs to ints.Your global is an array of ptrs to ints. So the return statement now returns a ptr (from the `&`) to an array of ptrs to ints.. That is it's just a ptr to the global. That's why `&array1` works and `array1` doesn't in the return statement. BTW, I misread the code originally. It doesn't have any arguments. – doug Jul 22 '20 at 16:58