Three students and four subjects respectively give their scores for the four subjects, and use the function to calculate the total score of each of them. That is, the return value of the function must be three.
If this question is implemented using a function, you must note that the space applied in the function body will be released after the function is executed. Pay attention to the lifecycle of local variables.
01
#include<stdio.h>void aver(int a[3][4],int b[3]){int i,j;for(i=0;i<3;i++){for(j=0;j<4;j++){b[i]+=a[i][j];}}}void main(){int a[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};int b[3]={0,0,0};int *q,i;q=b;aver(a,b);for(i=0;i<3;i++){printf("%d\n",*(b+i));}}
02
A one-dimensional array defined in a function degrades to a pointer, and a two-dimensional array degrades to a pointer pointing to a one-dimensional array. Therefore, you can make the following minor changes to the program.
# Include <stdio. h> void aver (INT (* A) [4], int * B) // degradation of the array {int I, j; for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) {B [I] + = A [I] [J] ;}} void main () {int A [3] [4] = {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5 }}; int B [3] = {0, 0}; int * Q, I; q = B; aver (a, B); for (I = 0; I <3; I ++) {printf ("% d \ n", * (B + I ));}}
You can get
Returns a pointer to an array as a form parameter.
3D array, degraded to a pointer to a two-dimensional array
A two-dimensional array degrades to a pointer to a one-dimensional array.
A one-dimensional array that degrades to a pointer to a type (such as an int ).
Function value transfer