码迷,mamicode.com
首页 > 编程语言 > 详细

【学习笔记】【C语言】结构体和函数

时间:2015-06-29 00:17:48      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <stdio.h>
 2 struct Student
 3 {
 4     int age;
 5     int no;
 6 };
 7 
 8 // 如果结构体作为函数参数,只是将实参结构体所有成员的值对应地赋值给了形参结构体的所有成员
 9 // 修改函数内部结构体的成员不会影响外面的实参结构体
10 void test(struct Student s)
11 {
12     s.age = 30;
13     s.no = 2;
14 }
15 
16 // 会影响外面的实参结构体
17 void test2(struct Student *p)
18 {
19     p->age = 15;
20     p->no = 2;
21 
22 }
23 
24 void test3(struct Student *p)
25 {
26     struct Student stu2 = {15, 2};
27     p = &stu2;
28     p->age = 16;
29     p->no = 3;
30 }
31 
32 int main()
33 {
34     struct Student stu = {28, 1};
35     
36     //test(stu);
37     //test2(&stu);
38     test3(&stu);
39     
40     printf("age=%d, no=%d\n", stu.age, stu.no);
41     
42     return 0;
43 }

 

 

【学习笔记】【C语言】结构体和函数

标签:

原文地址:http://www.cnblogs.com/dssf/p/4606325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!