1

I can't seem to understand why this won't work.

Global variable:

int genericSet[] = {1,2,3,4};

int main()...

int* function() {
    return &genericSet;
}

Why would it give me an error while trying to return this array? Is it because it's a global variable, if so why would that matter? How would I normally return a statically declared array? I realize this is extra work returning a global variable, but does it actually prevent this? I just used it as a place holder and kept getting an error.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
  • It may be helpful to read http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c about the differences between arrays and pointers. – Cubbi May 12 '11 at 03:00

5 Answers5

6

Your function type doesn't match the return type. Your function is int*, but what you're returning is of type int**.

arsenm
  • 2,903
  • 1
  • 23
  • 23
  • Spot on. Need to `return genericSet` instead of `return &genericSet`. – tylerl May 12 '11 at 02:44
  • Thank you, but have one more question, when I try to return the address as &genericSet and set the return value as int(-asterisk here won't display)[4] funciton... should this work for returning the adress of the array, because it wont' let me use int*-another asterisk here won't display either -- as the return value for &genericSet either :-/ – rubixibuc May 12 '11 at 02:57
  • 2
    @rubixibuc that would be `int (*function())[4] { return &genericSet; }` – Cubbi May 12 '11 at 03:05
  • Thank you that makes sense :-) when does the syntax require you to use the function name as part of the variable return type. I know it can be done for function pointers, but why do we do this for pointers to arrays? Is it becuase the function name is always part of the return type, but here it needs precedence in the order of operations – rubixibuc May 12 '11 at 03:08
5

I have no idea why everyone is answering that &genericSet is an int**; it isn't. To clarify:

int genericSet[] = {1,2,3,4};
typedef int (*ArrayPtr)[4];
int* f1() { return genericSet; }
ArrayPtr f2() { return &genericSet; }
servn
  • 3,049
  • 14
  • 8
2

You return the address of a variable, which is an array.

It can be seen as returning a pointer to a pointer. You do not need the &, simply return the array.

So either:

int ** function()
{
    return ( int ** )&genericSet;
}

or:

int * function()
{
    return genericSet;
}
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • It wont let me use int*-asterisk here won't display -- to return &genericSet, it give me an error it says it must be int(*-asterisk here won't display)[4] but this doesn't work either? – rubixibuc May 12 '11 at 03:02
  • You can safely cast it to ( int * * ), so you will code: `return ( int * * )&genericSet;` – Macmade May 12 '11 at 03:07
  • should be : `int (*)function()[4] { return &genericSet; }` –  May 12 '11 at 04:13
1

Just return genericSet without the & in front of it. It's already a pointer type by virtue of it having [] as part of its type.

Pointer/array similarities in C++ are important to understand, so I'd recommend you read up on that as part of your journey through the language.

Brian Kelly
  • 19,067
  • 4
  • 53
  • 55
0

genericSet itself is already an int *; &genericSet is an int **, a pointer to pointer to int.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186