Thread: passing arrays to a function

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    Question passing arrays to a function

    I am having a problem with how to pass the data collected in one function as an array to the next function.

    I collected the amount of items with my first fuction as follows:

    void input_items ( int item[], float amount[], int n)
    {
    /* declaration of local variable */
    int count;
    /* promt for amount according to item using loop*/

    for (count = 0; count <= n - 1 ; ++count)

    {
    printf ("Enter total of each item you want %i ", item[count]);
    scanf ("%f", &amount[count]);
    }

    }


    in my next function I want to take the amount of each item and multiply it by the item cost. But the problem I am having is with how do I pass the amount of each item collected in the input_items function to the next function used in the program.

    Do I need to return if I want to pass it to another function? I thought you didnt have to return anything if you passed it by reference. I am not sure of the syntax to pass it by reference.

    caws

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    
    void func( unsigned int array[10] )
    {
    	int x = 0;
    	for( x = 0; x < 10; x++ )
    		array[x] = x * x;
    }
    
    int main (void)
    {
    	unsigned int array[10] = {0};
    	int x = 0;
    
    	func( array );
    
    	for( x = 0; x < 10; x++ )
    		printf("array[%d] is %u\n", x, array[x] );
    
    	return 0;
    }
    Play with that example.

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

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Caws...

    In C arrays are never passed by value. Arrays are always passes by reference (a pointer to the array). Also it is important to understand that you can not pass the size of the array by putting it in the brackets as was done in the previous example. Most of programmers I have read would declare func like this...

    Code:
    void func( unsigned int array[] );
    or this...

    Code:
    void func( unsigned int *array );
    The important point to understand is that all three methods accomplish the same thing.

    You can make this function more robust by passing the size of the array as a second parameter...

    Code:
    void func( unsigned int *array, unsigned int size );
    Example:

    Code:
    #include <stdio.h>
    
    #define NUMBER_OF_ELEMENTS 10
    
    void func( unsigned int *array, int size )
    {
    	int x = 0;
    	for( ; x < size; x++ )
    		array[x] = x * x;
    }
    
    int main (void)
    {
    	unsigned int array[NUMBER_OF_ELEMENTS] = {0};
    	int arraysize = sizeof( array )/sizeof( unsigned int );
    	int x = 0;
    
    	func( array, arraysize );
    
    	for(; x < arraysize; x++ )
    		printf("array[%d] is %u\n", x, array[x] );
    
    	return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Rog
    Caws...
    Also it is important to understand that you can not pass the size of the array by putting it in the brackets as was done in the previous example.
    Bull$$$$. You can do it. It is legal. Show me how and why it isn't. You must specify at least the final argument in a multi-dimensional array. Passing it in a single dimension array is purely optional. There is nothing that prevents you from doing so. There is nothing wrong with doing so. It is valid. Prove me wrong and I'll admit it.

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

  5. #5
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    Also it is important to understand that you can not pass the size of the array by putting it in the brackets as was done in the previous example.
    If you pass it in this way,there is no problem but you will always get a const pointer to the first element of array,thats why usually programmers omit this and pass size as second argument!

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    Talking

    I have to say thank you to all of you. With your help I now grasp the passing by reference an array from one function to another and I completed my program.

    I think this web board is a great way to help me continue my learning of c-programming. Its great to examine the examples and read the questions and answers. It really helped me.

    Thanks again..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing A Function From One Object to Another
    By HalNineThousand in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2008, 07:26 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Passing 2d arrays to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2005, 09:30 AM