C-language structure as a function parameter

Source: Internet
Author: User

1) using struct variables as arguments to functions

When a struct variable is used as an argument of a function, the value is passed, and the contents of the memory unit of the structure variable are passed to the formal parameter in full order, and the parameter must be of the same type structure body variable

Demo

1# include <stdio.h>2# include <stdlib.h>3 4 //Create a student structure5 structStudent6 {7     Charname[ -];8     floatfscore[3];9}student={"Dire",98.5,89.0,93.5};//initializing struct-body variablesTen  One voidDisplay (structStudent su)//formal parameters are the same type of structure (student structure) A { -printf"-----information------\ n"); -printf"name:%s", su.name); theprintf"chinese:%.2f\n", su.fscore[0]); -printf"math:%.2f\n", su.fscore[1]); -printf"english:%.2f", su.fscore[2]); -printf"average score is:%.2f\n", (su.fscore[0]+su.fscore[1],su.fscore[2])/3); + } -  + intMain () A { at      - Display (student); -      -     return 0; -}

Printf:

2) Use pointers to struct variables as function arguments

Demo:

1# include <stdio.h>2# include <stdlib.h>3 4 structStudent {5     Charname[ -];6     floatfscore[3];7}student = {"Dire",98.5,89.0,93.5};//initializing struct-body variables8 9 Ten voidDisplay (structStudent *pstruct) One { Aprintf"------information-------\ n"); -printf"name:%s\n",pstruct->name); -printf"chinese:%.2f\n", (*pstruct). fscore[0]); theprintf"math:%.2f\n", (*pstruct). fscore[1]); -printf"english:%.2f\n", pstruct->fscore[2]); - } -  +  - intMain () + { ADisplay (&student);//passing the first address of a struct variable into the pstruct pointer variable as an argument at      -     return 0; -}

3) Use members of struct variables as function parameters

This is the same way that a function passes a parameter as an argument to a normal variable, which is the value passed

C-language structure as a function parameter

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.