Structure Variable, structure pointer variable, structure array as the function parameter Application Example Analysis

Source: Internet
Author: User
Structure Variable, structure pointer variable, structure array as the function parameter Application Example Analysis 

Struct stud

{Long int num;

Float score;

};

/* Struct variables are used as function parameters. The modified Member values cannot be returned to the main function */

Void funvr (struct stud T)

{T. num= 2000101;

T. Score = 71.0;

}

/* The struct array is used as a function parameter. The modified element value can be returned to the main function */

Void funar (struct stud T [])

{T [0]. num = 3000101;/* pay attention to the Reference Form of the elements in the struct array */

T [0]. Score = 81.0;

T [1]. num = 3000102;

T [1]. Score = 82.0;

}

/* The struct pointer variable is used as a function parameter. The modified struct value can be returned to the main function */

Void funpr (struct stud * t)

{T-> num = 4000101;/* pay attention to the specific form of referencing members through struct pointer variables */

(* T). Score = 92.0;

}

/* Call the above functions to modify the Member values respectively in the main function, and then verify the correctness of the result */

Main ()

{Struct stud a [2] = {1000101, 61.0}, {1000102, 62.0}, B = A [0], * P;

Printf ("Old B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

/* Display the original value of the member of struct variable B */

Funvr (B );

/* Verify the first case. observe and analyze the results. When the struct variable is used as the function parameter, the change in the value of the struct variable can affect the value of the struct variable, the following is the result value after calling function funvr (B */

Printf ("Call funvr () New B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

Funpr (& B);/* Use Pointer pairs of struct variables as function parameters */

Printf ("Call funpr () New B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

/* Output the original member value of Element A of the struct array */

Printf ("old A [0]: A [0]. num: % LD \ ta [0]. score: % F \ n ", a [0]. num, a [0]. score );

Printf ("old A [1]: A [1]. num: % LD \ ta [1]. score: % F \ n ", a [1]. num, a [1]. score );

/* Use struct array A as the function parameter, and then output the value of the member of the element. The value has been modified */

Funar ();

Printf ("new A [0]: A [0]. num: % LD \ ta [0]. score: % F \ n ", a [0]. num, a [0]. score );

Printf ("new A [1]: A [1]. num: % LD \ ta [1]. score: % F \ n ", a [1]. num, a [1]. score );

}

 

[Case Analysis]

1) when the struct variable is used as the function parameter [real participation parameter], the change of the member value of the struct variable does not affect the change of the member value of the corresponding real parameter.

2) When the struct array or struct pointer variable is used as the function parameter [real participation parameter, the change in the member value of the element [or variable pointed to by the pointer of the structure of the form parameter] will affect the change in the member value of the corresponding [or variable pointed to by the pointer of the structure of the real parameter ]..

3) struct variables can be used as function parameters. A struct type data can be returned by a function.

4) P = & B; point the struct pointer Variable P to the space of struct variable B.

P-> num: Used to reference the member num of struct variable B.

5) P = A; or P = & A [0]; points the struct pointer variable to struct array. Then:

① P-> num: the value of num, a member of the array element of the struct, is referenced by a pointer variable.

② P-> num ++: indicates that the pointer variable first references the member num value of the struct array element, and then adds 1 to the member num value of the element, first reference its value and then add 1 to it.

③ + P-> num: Adds 1 to the value of member num of the element to be referenced.

6) P = A; or P = & A [0]; indicates pointing the struct pointer Variable P to struct array.

① (P ++)-> num: indicates that the value of num, a member of the element of the struct array, is referenced through the pointer variable first, and then 1 is added to the pointer variable itself, adding 1 to the pointer variable indicates that the pointer variable points to the next element of the struct array.

② (++ P)-> num: First add 1 to the pointer variable itself, and first point the pointer variable to the next element of the struct array, REFERENCE The member num value of the struct array element pointed to by the pointer variable.

Structure Variable, structure pointer variable, structure array as the function parameter Application Example Analysis 

Struct stud

{Long int num;

Float score;

};

/* Struct variables are used as function parameters. The modified Member values cannot be returned to the main function */

Void funvr (struct stud T)

{T. num= 2000101;

T. Score = 71.0;

}

/* The struct array is used as a function parameter. The modified element value can be returned to the main function */

Void funar (struct stud T [])

{T [0]. num = 3000101;/* pay attention to the Reference Form of the elements in the struct array */

T [0]. Score = 81.0;

T [1]. num = 3000102;

T [1]. Score = 82.0;

}

/* The struct pointer variable is used as a function parameter. The modified struct value can be returned to the main function */

Void funpr (struct stud * t)

{T-> num = 4000101;/* pay attention to the specific form of referencing members through struct pointer variables */

(* T). Score = 92.0;

}

/* Call the above functions to modify the Member values respectively in the main function, and then verify the correctness of the result */

Main ()

{Struct stud a [2] = {1000101, 61.0}, {1000102, 62.0}, B = A [0], * P;

Printf ("Old B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

/* Display the original value of the member of struct variable B */

Funvr (B );

/* Verify the first case. observe and analyze the results. When the struct variable is used as the function parameter, the change in the value of the struct variable can affect the value of the struct variable, the following is the result value after calling function funvr (B */

Printf ("Call funvr () New B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

Funpr (& B);/* Use Pointer pairs of struct variables as function parameters */

Printf ("Call funpr () New B: B. Num: % LD \ TB. Score: % F \ n", B. Num, B. Score );

/* Output the original member value of Element A of the struct array */

Printf ("old A [0]: A [0]. num: % LD \ ta [0]. score: % F \ n ", a [0]. num, a [0]. score );

Printf ("old A [1]: A [1]. num: % LD \ ta [1]. score: % F \ n ", a [1]. num, a [1]. score );

/* Use struct array A as the function parameter, and then output the value of the member of the element. The value has been modified */

Funar ();

Printf ("new A [0]: A [0]. num: % LD \ ta [0]. score: % F \ n ", a [0]. num, a [0]. score );

Printf ("new A [1]: A [1]. num: % LD \ ta [1]. score: % F \ n ", a [1]. num, a [1]. score );

}

 

[Case Analysis]

1) when the struct variable is used as the function parameter [real participation parameter], the change of the member value of the struct variable does not affect the change of the member value of the corresponding real parameter.

2) When the struct array or struct pointer variable is used as the function parameter [real participation parameter, the change in the member value of the element [or variable pointed to by the pointer of the structure of the form parameter] will affect the change in the member value of the corresponding [or variable pointed to by the pointer of the structure of the real parameter ]..

3) struct variables can be used as function parameters. A struct type data can be returned by a function.

4) P = & B; point the struct pointer Variable P to the space of struct variable B.

P-> num: Used to reference the member num of struct variable B.

5) P = A; or P = & A [0]; points the struct pointer variable to struct array. Then:

① P-> num: the value of num, a member of the array element of the struct, is referenced by a pointer variable.

② P-> num ++: indicates that the pointer variable first references the member num value of the struct array element, and then adds 1 to the member num value of the element, first reference its value and then add 1 to it.

③ + P-> num: Adds 1 to the value of member num of the element to be referenced.

6) P = A; or P = & A [0]; indicates pointing the struct pointer Variable P to struct array.

① (P ++)-> num: indicates that the value of num, a member of the element of the struct array, is referenced through the pointer variable first, and then 1 is added to the pointer variable itself, adding 1 to the pointer variable indicates that the pointer variable points to the next element of the struct array.

② (++ P)-> num: First add 1 to the pointer variable itself, and first point the pointer variable to the next element of the struct array, REFERENCE The member num value of the struct array element pointed to by the pointer variable.

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.