Function value transfer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.