Thread: passing array to function

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    passing array to function

    Hi,
    If I want to pass an array to a user-defined function, do I pass the array with it's index inside it,
    Code:
    int Func(int score[num_of_scores])
    or do I have to pass the index separately?
    Code:
    int Func (int score[], int num_of_scores)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >or do I have to pass the index separately?
    Short answer: Yes.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    So thats all?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why don't you try it and find out?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    ok. I don't know if im doing this right. It compiles but comes out all wrong.
    Code:
    #include <stdio.h>
    #define SIZE 5
    void Func(int,int,int);
    
    main()
    
    {
    
    	int array[SIZE] = {1,2,3,4,5},
    		index=0,
    		a=0;
    
    		Func(index,array[SIZE],a);
    		
    }
    
    void Func(int index,int array[SIZE],int a)
    
    {
    
    	for(index=0; index<5; ++index)
    	{
    		a = array[index];
    		printf("\n%d" , a);
    	}	
    			
    			
    
    	
    }

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    You dont need to use square brackets, or specify the size of the array when you pass an array, therefore:

    Func(index,array[SIZE],a);

    becomes

    Func(index,array,a);

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Quote Originally Posted by Welshy
    You dont need to use square brackets, or specify the size of the array when you pass an array, therefore:

    Func(index,array[SIZE],a);

    becomes

    Func(index,array,a);
    that is correct (you got it first Welschy )

    also, you dont have to assign the number of elements of a unidimensional array when passing it to a function:

    Code:
     void Func(int index,int array[SIZE],int a)
    could be

    Code:
     void Func(int index,int array[],int a)

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thanks,
    It does need to be in the definition header right?
    edit> At least the brackets?
    Last edited by richdb; 03-02-2006 at 05:08 PM.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, it needs the brackets in the definition and prototype.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I do get these warnings:

    int ' differs in levels of indirection from 'int [5]' (line 13)
    'Func' : different types for formal and actual parameter 2 (line 13)
    formal parameter 2 different from declaration (line 19)

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Quote Originally Posted by SlyMaelstrom
    Yes, it needs the brackets in the definition and prototype.
    did you mean definition and declaration?

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    As I said, you need the brackets in the prototype.
    Code:
    void Func(int,int [],int);
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Ok. I've never seen that done before

    Thanks that kills the warnings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM