Tuesday, 15 April 2014

c - Is this the right way to pass an array back from the function to main -



c - Is this the right way to pass an array back from the function to main -

the programme running without errors, stops after runs function since seems array not beingness passed back. right way pass array function? i've included essential code below. please point out might wrong.

int matchednumbersfunc(int lotonumbers[],int ticketnumbers[], int *numbersright); int main() { int lotonumbers[7]; int ticketnumbers[7]; int matchednumbers[7]; int numbersright = 0; matchednumbers[7] = matchednumbersfunc(ticketnumbers, lotonumbers, &numbersright); homecoming 0; } int matchednumbersfunc(int lotonumbers[],int ticketnumbers[], int *numbersright) { int matchednumbers[7]; int i, k ,j; *numbersright = 0; for(k=0;k<7;k++) { matchednumbers[k] = 0; } for(i=0;i<7;i++) { for(j=0;j<7;j++) { if(ticketnumbers[i] == lotonumbers[j]) { matchednumbers[i] = ticketnumbers[i]; } } if(matchednumbers[i] != 0) { printf("you have winning number: %d\n", matchednumbers[i]); *numbersright = *numbersright + 1; if(matchednumbers[6] != 0) { *numbersright = *numbersright - 1; } } } homecoming matchednumbers[7]; }

so, returning single value rather array. , happens, returning value off end of array, , assigning value off end of array. arrays have 7 elements, indexed 0 6.

what need pass array function , allow function populate it. simple example:

void foo(int array[], size_t count) { (size_t = 0; < count; i++) array[i] = i; }

which phone call this:

int myarray[7]; foo(myarray, 7);

c arrays function

No comments:

Post a Comment