Hi,
If I want to pass an array to a user-defined function, do I pass the array with it's index inside it,or do I have to pass the index separately?Code:int Func(int score[num_of_scores])
Code:int Func (int score[], int num_of_scores)
Printable View
Hi,
If I want to pass an array to a user-defined function, do I pass the array with it's index inside it,or do I have to pass the index separately?Code:int Func(int score[num_of_scores])
Code:int Func (int score[], int num_of_scores)
>or do I have to pass the index separately?
Short answer: Yes.
So thats all?
Why don't you try it and find out?
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);
}
}
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 :D)Quote:
Originally Posted by Welshy
also, you dont have to assign the number of elements of a unidimensional array when passing it to a function:
could beCode:void Func(int index,int array[SIZE],int a)
Code:void Func(int index,int array[],int a)
Thanks,
It does need to be in the definition header right?
edit> At least the brackets?
Yes, it needs the brackets in the definition and prototype.
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)
did you mean definition and declaration?Quote:
Originally Posted by SlyMaelstrom
As I said, you need the brackets in the prototype.Code:void Func(int,int [],int);
Ok. I've never seen that done before :eek:
Thanks that kills the warnings.