Sending array to new function to reference

Hey, I've looked all over and can't seem to find a thread relevant to my issue that has explanations I can understand. I created an array in my main call program StartProg() and I can't seem to figure out how to send it to a new function GetStudentScore().

1
2
3
4
5
6
7
8
9
10
11
12
void StartProg() {
	int examScores[3];
	int returnScore = GetExamScores(examScores[3]);
}


int GetExamScores(examScores[3])
{
/* code */
return intVar;
}


Then in my .h I have:
1
2
void StartProg();
int GetExamScores(int[]);


Is there something I am doing wrong? I have these related errors:

Error 1 error C2664: 'GetExamScores' : cannot convert parameter 1 from 'int' to 'int []' c:\users\watus_000\documents\my box files\documents\school\_cs1410\proj_02\proj_02\proj_02.cpp 23 1 Proj_02
2 IntelliSense: argument of type "int" is incompatible with parameter of type "int *" c:\Users\watus_000\Documents\My Box Files\Documents\School\_CS1410\Proj_02\Proj_02\Proj_02.cpp 22 34 Proj_02
Last edited on
Firstly, your parameter on line 7 is missing a type.

Secondly, examScores[3] is returning the value in the array at index 3 (which is out of bounds).

How to pass an array:
1
2
3
4
5
6
7
8
9
10
void funky(int arr[5]){
   for(size_t I(0); I < 5; ++I)
      cout << arr[I] << ' ';
   cout << '\n';
}

//...

int arr[5] = {1, 2, 3, 4, 5};
funky(arr);


But you should consider having a second parameter to pass the size of the array.
Topic archived. No new replies allowed.