Hey,
I started learning C++ and now I need to return an array from a function, I already noticed that just returning it as if it was a ormal variable wouldn't work.
Now I wanted to return a pointer to the array but that still gave me compiling errors:

Code:
int function(int array[])
    {
    //array stuff here
    int *pointer = &array;
    return pointer;
    }
Terminal output:
Code:
main.cpp:6:21: Fehler: »int**« kann nicht nach »int*« in Initialisierung umgewandelt werden
main.cpp:8:12: Fehler: ungültige Umwandlung von »int*« in »int« [-fpermissive]
int* function(...){...}
elfprince13 wrote:
int* function(...){...}
It still gives me this error:
Code:
main.cpp:6:21: Fehler: »int**« kann nicht nach »int*« in Initialisierung umgewandelt werden
(Sorry that it is all german... :/)
The "array" variable is already a pointer, so remove the & in front of it.
christop wrote:
The "array" variable is already a pointer, so remove the & in front of it.
Ok, that is working now, thanks a LOT.
But now I got the problem how to call the function, what i did is:

Code:
array = &function(array);
and terminal gave me this:
Code:
main.cpp:104:65: Fehler: Als Operand für unäres »&« wird L-Wert erfordert

Code:
int* function(int array[])
    {
    return array;
    }

Honestly, this:

Code:
void function(int array[]) {}

is the same as:

Code:
void function(int *array) {}
But i thought void doesn't return a thing?
Yes, it's as Christop said:

Code:
int x;
int a[]; // basically the same as int* a;
...
x; // This gives an int value
&x; // This gives an int* value
a; // This gives an int* value
&a; // This gives an int** value

This should do it. Even though your error messages are in German, I can see it comparing "int" to "int*" (because your function returned "int", but pointer was an "int*") [fixed], and comparing "int*" to "int**" (because pointer is an "int*" and "&array" gives an "int**"). When you see stuff about mismatching types, try to figure what types it's referring to and why they clash (or which types might not be what you think).

EDIT: (...removed some "confusing stuff"...)
Sorunome wrote:
But i thought void doesn't return a thing?

void doesn't return anything. I did that and gave it an empty function body so you would look at what arguments were being passed in. I was trying to get the point across that "int array[]" is the same is "int *array".
shkaboinka wrote:
Yes, it's as Christop said:

Code:
int x;
int a[];
...
x; // int value
&x; // int* value
a; // int* value
&a; // int** value

This should do it. Even though your error messages are in German, I can see it comparing "int" to "int*" (because your function returned "int", but pointer was an "int*") [fixed], and comparing "int*" to "int**" (because pointer is an "int*" and "&array" gives an "int**"). When you see stuff about mismatching types, try to figure what types it's referring to and why they clash (or which types might not be what you think).

"array" by itself gives an array address just as "pointer" by itself would give an int (or int array) address. It is setup so that static arrays ("type var[]") and pointer-arrays ("type* var") use the same syntax ("var[index]" for indexing, "var" for the array address, and "var+index" for the address of "var[index]" [basically just add the index to the address directly; so this is the same as "&(var[index])" because "var[index]" is "(*var)+index" if you think about it...])
Sorry, I don't really get it, could someone please post a code example how to make a function return a array and how to call that function then correctly? Thanks!
I think this should work:

Code:

void main(void) {
 int[] arr;
 doStuff(arr);
}
int* doStuff(int* array) {
 return array;
}
Thanks, I didn't try that out because I already just found a solution, so that it returns the pointer which is stored into a variable and then into an array Smile
Sorunome wrote:
christop wrote:
The "array" variable is already a pointer, so remove the & in front of it.
Ok, that is working now, thanks a LOT.
But now I got the problem how to call the function, what i did is:

Code:
array = &function(array);
and terminal gave me this:
Code:
main.cpp:104:65: Fehler: Als Operand für unäres »&« wird L-Wert erfordert

I feel like you should just be doing:

Code:
array = function(array);

It's returning a pointer, and that's what you want. No need for the &.
^that didn't work to me, i first had to store that into a new pointer so something like this:

Code:
int *temp;
temp = function(array);
array = &temp;

EDIT: No, that suddenly doesn't work anymore, gives me

Code:
main.cpp:116:71: Fehler: expected primary-expression before »]« token


EDIT2:
This doesn't work aswell:

Code:
array = function(array);
I'd need to see all the code to really understand.
Well, Jacobly just told me if you modify a array inside a function the original one is also modifide so I don't need that at all... *facepalm*
EDIT: But thanks for all your help anways! Smile
Sorunome wrote:
Well, Jacobly just told me if you modify a array inside a function the original one is also modifide so I don't need that at all... *facepalm*
EDIT: But thanks for all your help anways! Smile

If you pass an array to a function, you're really passing a pointer (aka a reference) to that array, so the function really modifies the original array in the first place. The array is not copied when the function is called or when it returns. You would have to explicitly create a new array and copy the old one to it (such as with malloc and memmove/memcpy) if you wanted a copy of the array.
Oh, ok, thanks! Smile
Part of the confusion is that "int x[]" and "int *x" are the same type (only the first kind is constant). C/C++ makes this confusing because the ACTUAL type is "int[]" and "int*". The reason that it is how it is is that, instead of putting "[]" or "*" on the TYPE, you put it on the VARIABLE name (and in the place where you would use it with the variable). Thus your original program should have looked like this:

Code:
int* function(int* array)
{
    //array stuff here
    return array;
}

Of course, as I just stated, you could replace "int* array" with "int array[]" and have it do the same thing (except that declaring it with "[]" means that you cannot assign it to a different array; though you can always change the contents of the array). ...Also of course (as you said), you don't need to return a value, since you modify the array indirectly (rather than copying it all).

I explained that the first time; only I explained too MUCH and made it confusing (thus voiding the parts that MIGHT have been helpful to you). I will reiterate that if the compiler complains about trying to do something with one type (an "int**" for example) where another type is expected (an "int*" for example), then that is your hint that something does not match. In that particular case, you can see that there is an extra "*", so try either adding a "*" to something or removing one from another. This might apply to "&" because giving the address of something means that it should be used with a pointer to something of the same type.

...Also, it seems that you are confused by syntax. If you declare a pointer (e.g. "int *x"), it is not always the case that you want to use the "&" operator on what you are assigning to it (e.g. "int *x = &y"; sometimes you want "int *x = y;" or something else). The point is to get the same type of value (the address of an int), which depends on what y is. The "&" operator means "the address of", and "*x" means "the value stored in that address (x)". An example:


Code:
int i; // just an int
int *x1; // an int pointer. Values are the ADDRESS of another int
int *x2; // another int-pointer
int y[]; // like an int*, but CANNOT BE CHANGED LATER
int **z; // an int* pointer (a pointer to an int-pointer). Values are the address of another int*

x1 = &i; // x1 now contains the address of i. The value stored in x is &i.

*x1 = 5; // You can read *x1 as "the value stored at (the address stored in x1)", which in this case is "the value stored in (address of i)" or just "the value stored in i". Thus, this changes i to 5

x1 = &x2; // ERROR!!! This is trying to store the address of an int-pointer in x1, rather than the address of an int. This is why you had issues. The CORRECT usage in this case is below:

x1 = x2; // copy the value in x2 to x1 (just as you'd expect). In this case, the value in x2 should be the address of an int, since it is an int-pointer. x1 and x2 will now "point" to the same int-variable now (until one of them is given a different address to point to).

x1 = *z; // Think about this: z points to the address of an int-pointer (it's an (int-pointer)-pointer), but x1 can only have the address of an int. So to go from an (int-pointer)-pointer to an int-pointer, we get the value stored in the address that z points. That is, x1 is an int*, and *x1 refers to an int; so if z is an int**, then *z refers to an int*, and **z refers to an int (think of that as *(*z)). You can also think of this as "pulling out" *'s from the type by putting them on the variable when it is used.

x1 = y; // That's fine, since they both store int-addresses

y = x1; // ERROR! (y cannot be reassigned).
y[i]; // the i-th element in y. What really happens is that y points to the first int in a whole LIST of int values, so *y is the same as y[0], and y[1] is the one after that, and so on.

x1[i] // means the same thing. You can point to just one int by itself, or you can point to some int in a whole LIST of ints.
In a function's parameter list, int *foo and int foo[] are identical. Both mean that foo is a pointer to int, and foo is modifiable in both cases. This is the only place where the two are the same and can be used interchangeably (it's really a stylistic choice--int foo[] tells other people that the function takes a pointer to an array, but int *foo just says that it's a pointer, which may or may not be to an array. The compiler doesn't care which one you use.).

Outside of a function's parameter list, an array is different from a pointer. A pointer is a single value (eg a 32 or 64-bit address), and an array is a block of memory. You can get the address of an array by leaving off the brackets. You cannot assign a value to the array's address, though, as shkaboinka mentioned. So this is illegal:
Code:
int array[3];
array = NULL;




Just in case that's not confusing enough, this code is actually legal:

Code:
int array[3];
2[array] = 7;

I occasionally use expressions like this when I feel like writing somewhat obfuscated code:

Code:
int x = 3; // any value between 0-15
char y = x["0123456789abcdef"]; // get the ascii character for the digit x

(The comments defeat the obfuscation, though.) Very Happy
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement