Thread: Pass an array to a function

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Pass an array to a function

    Can someone please narrate this code for me? I do not understand why we create:
    1. void testfunc (int a[]) before main()
    2. testfunc ( a) - in main()
    3. void testfunc again after main.

    can someone please explain the purpose of these? Thank you

    Code:
    void testfunc (int a[]);
    
    
    void main()
    {
    	int ctr, a[5] = {1,3,5,7,11};
    	testfunc(a);
    	for (ctr = 0; ctr < 5; ctr = ctr + 1)
    	{
    		printf("%d\n", a[ctr]);
    	}
    }
    
    
    void testfunc ( int a[] )
    {
    	int n;
    	for(n = 0; n < 5; n = n + 1)
    	{
    		a[n] = a[n] * a[n];
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try doing your own homework first. If you make an effort, people might be willing to give pointers.

    And, unrelated to your question, main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You seemed to have an idea of what these things were back in August.

    Program with two source files - (Post #9, confirmed in posts #12 and #15)

    Tell us what you think, and we'll tell you if you're on the right track.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you pass an array into a function??
    By workisnotfun in forum C Programming
    Replies: 9
    Last Post: 11-18-2012, 09:47 PM
  2. Pass 2d array to function
    By binks in forum C Programming
    Replies: 5
    Last Post: 06-05-2012, 06:14 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Is it possible to pass an array to a function?
    By Loic in forum C++ Programming
    Replies: 20
    Last Post: 05-06-2007, 10:04 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread