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)
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.
My best code is written with the delete key.
So thats all?
Why don't you try it and find out?
Sent from my iPadŽ
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 )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?
Last edited by richdb; 03-02-2006 at 05:08 PM.
Yes, it needs the brackets in the definition and prototype.
Sent from my iPadŽ
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?Originally Posted by SlyMaelstrom
As I said, you need the brackets in the prototype.Code:void Func(int,int [],int);
Sent from my iPadŽ
Ok. I've never seen that done before
Thanks that kills the warnings.