Thread: Pass array through functions

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    Pass array through functions

    Hey, i would like to know how exactly you pass an array from one function to another. so say i generated 10 random numbers and stored them in an integer array, how would i return this to a function. and then also, how would i pass this array directly to another function? thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    #include <stdio.h>
    
    void foo(int *array, int arraysize)
    {
    	int	i;
    
    	/* Print the contents of the array */
    	for(i = 0; i < arraysize; i++)
    		printf("Array index %d = %d\n", i, array[i]);
    }
    
    int main(int argc, char* argv[])
    {
    	int array[10], i, size;
    
    	/* Get the size of the array */
    	size = sizeof(array) / sizeof(int);
    
    	/* Fill the array with some data */
    	for(i = 0; i < size; i++)
    		array[i] = i;
    
    	/* Call a function, passing it the array, and its size */
    	foo(array, size);
    
    	return 0;
    }

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    thank you very much.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    so what if i wanted to get the numbers in the array generated randomly and then returned? i know that foo would have to be an int instead of void..
    Code:
    array = foo(user_designated_number_for_size);
    ?
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    int* foo(int *array, int arraysize)
    {
    	int	i;
    
    	/* Print the contents of the array */
    	for(i = 0; i < arraysize; i++)
    		printf("Array index %d = %d\n", i, array[i]);
    
    	return array;
    }

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    hmm.. that helps. but i was thinking a function more like this:
    Code:
    int* generate(int number)
    {
         int maxrand = 70, random_number[255], counter = 0;
         /* Clear the menu from the screen, multiple menu's annoy me */
         system("cls");
         
         /* create random number */
         do {
         srand( (unsigned)time( NULL ));
         random_number[counter] = rand() % maxrand;
         ++counter;
         } while (counter < number);
         printf("\nRandom number generation completed.\n\n");
         
         /* send the random number back to main */
         return random_number;
    }
    i'm getting an error saying that the functions returns the address of a local variable which makes sense. so would this be one of those extreme cases to use global variables? because its seeming that they would make this A LOT easier. if not, why? whats bad about global variables? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Changing 1 line in bithub's code
    Code:
    int* foo(int *array, int arraysize)
    {
    	int	i;
    
    	/* fill the contents of the array with random numbers */
    	for(i = 0; i < arraysize; i++)
    		array[i] = rand() % maxrand;
    
    	return array;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah except you still can't make an assignment as they're trying to do:

    Quote Originally Posted by willc0de4food
    so what if i wanted to get the numbers in the array generated randomly and then returned? i know that foo would have to be an int instead of void..
    Code:
    array = foo(user_designated_number_for_size);
    ?
    You cannot assign arrays. In other words, if this is what you're trying to do, you can't:
    Code:
    int *foo( ... )
    {
        ...
    }
    
    ...
    
    int main( void )
    {
        int array[ BUFSIZ ];
    
        array = foo( ... );
    
        return 0;
    }
    You cannot assign arrays entire values like that. If 'array' were a pointer that you had, in function foo, malloced and filled, then you could return it:
    Code:
    int *foo( ... )
    {
        int *bar;
    
        bar = malloc( somesize );
        ...
    
       return bar;
    }
    
    int main( void )
    {
        int *baz;
    
       baz = foo( ... );
       ...
    
        free( baz );
        return 0;
    }
    This is legal. The first example isn't. Because again, you cannot use the assignment operator (other than at declaration) to assign values to entire arrays.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    what does malloc do? also, what does calloc do?

    and what about global variables? why are they bad? lol thanks.
    Registered Linux User #380033. Be counted: http://counter.li.org

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    malloc and calloc allocate memory. You really should read some C tutorials before you go any further.

  11. #11
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol ok. but that still doesn't say anything about global variables thats what i would really like to know about. lol
    Registered Linux User #380033. Be counted: http://counter.li.org

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    There's nothing wrong with global variables, but use them only when you have to. If you can accomplish the same thing easily without using a global variable, then it's considered better programming style to do it that way.

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    iIi c...well what if a global variable would make everything 50% easier? that could be an exaggeration, but i belive it would simplify things greatly - especially considering my skill level is that of a novice.
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by willc0de4food
    well what if a global variable would make everything 50% easier? that could be an exaggeration, but i belive it would simplify things greatly - especially considering my skill level is that of a novice.
    Using global variables can make things appear to be 50% easier as a novice. Unfortunately, this little habit grows with you and when you have more complicated code, it can make things 500% harder to debug (remember that 89.6% of all statistics are made up on the spot). This tradeoff is one of the reasons why the recommendation against using global variables is so prevalent. It also detracts from learning to think in terms of encapsulation and against properly choosing data types.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 07:06 AM
  4. So this is how you pass arrays to functions!
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 03:08 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM