Function to implement structural variable exchange value

xiaoxiao2021-03-06  43

#include #include struct stuff {char full_name [20]; unsigned age; unsigned gender;}; void swap_struct_data (char * ptr_a, char * ptr_b, int size) {/ * function: Swap_struct_data effect: swap the value parameters of two structural variables: the address of the PTR_A variable A, the address of the PTR_B variable B, the size of the size * / char p; while (size -! = 0) {P = * PTR_A; * PTR_A = * PTR_B; * PTR_B = P; PTR_A ; PTR_B ;}} INT Main () {/ * Defines two stuff type variables, and initializes * / struct stuff stu_a; struct stuff stu_b;

STRCPY (stu_a.ffull_name, "hexuhui"); stu_a.ge = 1;

STRCPY (stu_b.full_name, "candy"); stu_b.age = 20; stu_b.gender = 2; / * Displays the value before the exchange data * / printf ("Data IN Stu_a: / N");

Printf ("% s / n", stu_a.full_name); Printf ("% D / N", STU_A.AGE); Printf ("% d / n", stu_a.gender); Printf ("Data IN Stu_b: / N ");

Printf ("% s / n", stu_b.ffull_name); Printf ("% d / n", stu_b.age); Printf ("% d / n", stu_b.gender); / * calls swap_struct_data function, remove STU_A And the address of the STU_B, the incoming function is converted into a character pointer, and SIZEOF (STRUCT STUFF) is the size of the structure * / swap_struct_data ((char *) & stu_a, (char *) & stu_b, sizeof (struct stuff)); / * Displays the value after the exchange data * / printf ("Data IN Stu_A After Call Function: / N");

Printf ("% s / n", stu_a.ffull_name); Printf ("% D / N", STU_A.AGE); Printf ("% D / N", stu_a.gender; Printf ("Data IN Stu_b After Call Function: / n ");

Printf ("% s / n", stu_b.ffull_name); Printf ("% d / n", stu_b.age); Printf ("% d / n", stu_b.gen);}

转载请注明原文地址:https://www.9cbs.com/read-75929.html

New Post(0)