c++ - Returning array -
how return pointer array , values that?
int * getchange(int first[], int second[], int size) { int = 0; int * change = new int[2]; (i = 0; < size; i++) { if (first[i] != second[i]) { change[0] = first[i]; change[1] = second[i]; } break; } return change; } function main() { int mynumbers[] = {1, 0, 2, 3}; int possibilities[] = {0, 1, 2, 3}; int * change; change = getchange(possibilities, mynumbers, 4); printf("%i / %i\n", (change), (change+1)); }
unfortunately function seems return addresses, not values...
try change
printf("%i / %i\n", (change), (change+1));
to
printf("%i / %i\n", *(change), *(change+1));
in printf
function need use int
parameters, not int*
. change
variable pointer. must use *change
and, using pointers arithmetics, *(change + 1)
obviously, don't forget free allocated memory.
Comments
Post a Comment