鱼C论坛

 找回密码
 立即注册
查看: 3199|回复: 12

[已解决]结构体指针函数的使用问题

[复制链接]
发表于 2019-8-11 16:36:59 | 显示全部楼层
       如果想在函数中修改结构体成员变量的值,那么,输入参数必须是这个结构体的指针
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <windows.h>


  4. struct programer {
  5.         char name[16] ;
  6.         int age       ;
  7.         int salary    ;
  8. }                     ;

  9. void add_salary4(struct programer * p , int num) {
  10.         p -> salary += num ;
  11. }

  12. void age_t(struct programer * p) {
  13.         p -> age = 29      ;
  14. }

  15. int main(void) {
  16.         struct programer xiaoniu                                                                              ;
  17.         strcpy(xiaoniu . name , "小牛")                                                                       ;
  18.         xiaoniu . age = 28                                                                                    ;
  19.         xiaoniu . salary = 20000                                                                              ;
  20.         printf("main函数 -> 姓名:%s, 年龄:%d, 薪水:%d\n", xiaoniu . name, xiaoniu . age, xiaoniu . salary) ;
  21.         age_t(& xiaoniu)                                                                                      ;
  22.         add_salary4(& xiaoniu , 10000)                                                                        ;
  23.         printf("main函数 -> 姓名:%s, 年龄:%d, 薪水:%d\n", xiaoniu . name, xiaoniu . age, xiaoniu . salary) ;
  24.         system("pause")                                                                                       ;
  25. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 14:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表