Thread: Performing operations on array returned from function?

  1. #1
    Registered User
    Join Date
    Mar 2002

    Performing operations on array returned from function?

    Hi, I am returning an array to main() from a function. Once it has been returned to main() how do I perform operations on this array, such as printing its contents, counting the occurrences of certain numbers or assigning some of its contents to another array?

    For example, say the function is called arrayfunction() and returns an array of 5 integers. I can't print the contents like this:
    Printf("Here is my array: %d", arrayfunction());
    because that would just print the first number of the array, right? And if I put it through a loop it would just keep printing the first number of the array. So how would I print the entire array?


  2. #2
    You have to loop and increment through array elements 0 to 4.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Thanks vVv but I couldn't get any of that to work Here is the code that I'm currently working on. I deleted some irrelevant parts:
    int playgame(int *dicearray, int ndice)
    	int i, randomnumber, getrandomnumber(), *rdarray = NULL;
    	rdarray = dicearray = (int*)malloc(ndice * sizeof(int));
    	for (i=0; i<ndice; i++)
    		dicearray[i] = 0;
    	for (i = 0; i < ndice; ++i)
    		randomnumber = getrandomnumber();
    		dicearray[i] = randomnumber;
    	for (i = 0; i < ndice; i++)
    	printf("%d ", dicearray[i]);
    	dicearray = NULL;
    	for (i = 0; i < ndice; ++i)
    	{/*prompt user */scanf("%d", &rdarray[i]);
    	return *rdarray;
    // so we are returning ndice number of integers. In main we have:
    int main(void)
    	int playgame(int *dicearray, int ndice);
    	int *dicearray = NULL;
    	int finalarray[5] = {0}, i;
    *finalarray = playgame(dicearray, 5);
    for(i = 0; i < 5; i++)
        printf(" %d", finalarray[i]);
    If it was working the way I hoped it would the 5 random numbers would be printed followed by the user's choice but instead only the first element of rdarray is printed. Obviously I'll be doing more useful things once I've got the concepts right. I tried incorporating some of vVv's suggestions but just got errors, my fault I'm sure .
    Last edited by myth; 03-22-2002 at 11:45 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Thanks for your advice; my code wasn't THAT much of a write-off though . getrandomnumber() is a separate function and there is a } immediatedly after the scanf(). I made the other amendments and am told:

    'playgame' : 'int *(int *,int )' differs in levels of indirection from 'int ()'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM