Returning and manipulating array/pointer from function to main
Author |
Message |
Magi #1 / 5
|
 Returning and manipulating array/pointer from function to main
I'm coding a game of Yahtzee (the dice game) and I have a function playgame which is supposed to send an array of random numbers back to main. When the numbers (e.g. 6,5,4,0,0) return to main I want to do things like print then and assign some of them to another array. I'm having problems doing that. Here is the playgame function that I'm working on. getrandomnumber and compare are separate functions: int *playgame(int *dicearray, int ndice, int throwx) { int compare (const void * a, const void * b); int i, randomnumber, getrandomnumber(), *rdarray = NULL; printf("Throw number %d\nshake...shake...shake...THROW!!!\n", throwx); rdarray = dicearray = (int*)malloc(ndice * sizeof(int)); for (i=0; i<ndice; i++) { dicearray[i] = 0; rdarray[i] = 0; } for (i = 0; i < ndice; ++i) { randomnumber = getrandomnumber(); dicearray[i] = randomnumber; } qsort (dicearray, ndice, sizeof(int), compare); printf("You have thrown ... "); for (i = 0; i < ndice; i++) printf("%d ", dicearray[i]); //free(dicearray); //dicearray = NULL; for (i = 0; i < ndice; ++i) { printf("\nNow please choose number %d to keep \n(or press 0 to throw the remaining numbers).\n", i + 1); scanf("%d", &rdarray[i]); } printf("You have kept ... \n"); for (i = 0; i < ndice; ++i) printf("%d ", rdarray[i]); return rdarray; Quote: }
Here are the relevant parts of main: int *dicearray = NULL; int *finalarray = {0}, i; finalarray = playgame(dicearray, 5, 1); for(i = 0; i < 5; i++) printf(" %d ", finalarray[i]); When compiled I get the error: error C2040: 'playgame' : 'int *(int *,int ,int )' differs in levels of indirection from 'int ()' I know the whole thing is a mess but all I really need to know is this: how do you return an array of integers from a function, and then perform operations on those returned numbers e.g. printing them and assigning them to another array. Thanks.
|
Thu, 09 Sep 2004 10:34:16 GMT |
|
 |
Eric Amic #2 / 5
|
 Returning and manipulating array/pointer from function to main
Quote: > I'm coding a game of Yahtzee (the dice game) and I have a function > playgame which is supposed to send an array of random numbers back to > main. When the numbers (e.g. 6,5,4,0,0) return to main I want to do > things like print then and assign some of them to another array. I'm > having problems doing that. Here is the playgame function that I'm > working on. getrandomnumber and compare are separate functions: > int *playgame(int *dicearray, int ndice, int throwx) > { [body snipped] > } > Here are the relevant parts of main: > int *dicearray = NULL; > int *finalarray = {0}, i; > finalarray = playgame(dicearray, 5, 1); > for(i = 0; i < 5; i++) > printf(" %d ", finalarray[i]); > When compiled I get the error: > error C2040: 'playgame' : 'int *(int *,int ,int )' differs in levels > of indirection from 'int ()'
Judging from the (frankly obscure) error message, I'd say that playgame() is not declared before its call in main(). As a result, the compiler assumes playgame() returns int when it encounters the call. Try adding int *playgame(int *, int, int); just before the definition of main(). The other option is to put the definition of playgame() before the definition of main().
|
Thu, 09 Sep 2004 12:56:56 GMT |
|
 |
Magi #3 / 5
|
 Returning and manipulating array/pointer from function to main
On Sun, 24 Mar 2002 04:56:56 GMT, "Eric Amick" Quote:
>Judging from the (frankly obscure) error message, I'd say that >playgame() is not declared before its call in main(). As a result, the >compiler assumes playgame() returns int when it encounters the call. >Try adding >int *playgame(int *, int, int); >just before the definition of main(). The other option is to put the >definition of playgame() before the definition of main().
I get exactly the same error message if I do that. Here is some earlier code that compiles fine but doesn't work as intended. The problem is that the printf in main() prints the first element of the array returned from playgame, but it is supposed to print the whole array. That is my real problem: how do I perform operations on an array returned from a function? int playgame(int *dicearray, int ndice, int throwx) { int compare (const void * a, const void * b); int i, randomnumber, getrandomnumber(), *rdarray = NULL; printf("Throw number %d\nshake...shake...shake...THROW!!!\n", throwx); rdarray = dicearray = (int*)malloc(ndice * sizeof(int)); for (i=0; i<ndice; i++) { dicearray[i] = 0; rdarray[i] = 0; } for (i = 0; i < ndice; ++i) { randomnumber = getrandomnumber(); dicearray[i] = randomnumber; } qsort (dicearray, ndice, sizeof(int), compare); printf("You have thrown ... "); for (i = 0; i < ndice; i++) printf("%d ", dicearray[i]); //free(dicearray); //dicearray = NULL; for (i = 0; i < ndice; ++i) { printf("\nNow please choose number %d to keep \n(or press 0 to throw the remaining numbers).\n", i + 1); scanf("%d", &rdarray[i]); } printf("You have kept ... \n"); for (i = 0; i < ndice; ++i) printf("%d ", rdarray[i]); return *rdarray; Quote: }
int main(void) { int playgame(int *dicearray, int ndice, int throwx); int *dicearray = NULL; int finalarray[5], i, o; srand(GetTickCount()); for(i = 0; i < 5; i++) finalarray[i] = 0; *finalarray = playgame(dicearray, 5, 1); for(i = 0; i < 5; i++) printf(" %d ", finalarray[i]);
|
Thu, 09 Sep 2004 13:58:42 GMT |
|
 |
Daniel Fo #4 / 5
|
 Returning and manipulating array/pointer from function to main
Quote: > int playgame(int *dicearray, int ndice, int throwx)
Didn't this function have a return type of int * in your previous post? Don't try to type in code. You'll make mistakes. Copy and paste if you have to. Quote: > { > int compare (const void * a, const void * b); > int i, randomnumber, getrandomnumber(), *rdarray = NULL;
Why are you declaring functions here? Quote: > printf("Throw number %d\nshake...shake...shake...THROW!!!\n", > throwx); > rdarray = dicearray = (int*)malloc(ndice * sizeof(int));
Don't cast the return of malloc(). Check for failure. Consider sizeof *rdarray instead of sizeof(int). Quote: > for (i=0; i<ndice; i++) > { > dicearray[i] = 0; > rdarray[i] = 0; > }
dicearray and rdarray came from the same malloc() call; thus they point to the same object. You only need to assign to one to assign to the other. Your second assignment is useless. [snippity snip] Quote: > int main(void) > { > int playgame(int *dicearray, int ndice, int throwx); > int *dicearray = NULL; > int finalarray[5], i, o; > srand(GetTickCount()); > for(i = 0; i < 5; i++) > finalarray[i] = 0; > *finalarray = playgame(dicearray, 5, 1);
I'm assuming you're trying to make playgame fill out the array 'finalarray'. Don't. If playgame() has allocated the storage for the array (and it has), just make finalarray an int *. You're trying to do more work than you have to. If you absolutely positively have to have another copy of the array, use memcpy(). And don't forget to free() the array returned by playgame(). -Daniel
|
Thu, 09 Sep 2004 14:57:21 GMT |
|
 |
Eric G. Mille #5 / 5
|
 Returning and manipulating array/pointer from function to main
I'd consider using a structure for the dice and just passing around a pointer to it. Removes unnecessary calls to malloc/free. #define NDICE 5 struct die { int keep; /* !0 -> keep die, 0 -> roll again */ int die; Quote: };
... struct die dice[NDICE]; ... roll_dice (dice); print_dice (dice); keep_dice (dice); ... void roll_dice (struct die dice[NDICE]) { int i; for (i = 0; i < NDICE; i++) { if (!dice[i].keep) dice[i].die = 1 + (rand() % 6); } Quote: }
...
|
Fri, 10 Sep 2004 07:13:34 GMT |
|
|
|