IOS开发学习笔记007-数据结构

目录:

1、全局变量和局部变量

2、结构体

3、结构体数组

4、结构体做函数参数

5、结构体指针

6、枚举

7、总结

一、全局变量和局部变量

全局变量和局部变量的区别

1、  全局变量,再函数外定义的变量

    作用范围:是从变量定义到文件结束

    默认初始值是0

2、  局部变量,再函数内部定义的变量

    作用域:从变量定义开始到函数结束

    没有默认初始值

代码示例

 1 #include <stdio.h>
 2 
 3 /*
 4     全局变量和局部变量
 5  */
 6 
 7 //全局变量,再函数外定义的变量
 8 //作用范围:是从变量定义到文件结束
 9 //默认初始值是0
10 
11 //局部变量,再函数内部定义的变量
12 //作用域:从变量定义开始到函数结束
13 //没有默认初始值
14 
15 
16 //全局变量
17 int a = 10;
18 
19 
20 
21 int main()
22 {
23     int *p = &a;//使用局部指针指向全局变量
24     //局部变量
25     int a = 20;
26 
27     a++;//这样使用,引用的时局部变量,再函数内部定义的局部变量如果和全局变量重名,那么局部变量会覆盖全局变量,如果要在函数内部使用全局变量,可以使用局部指针指向全局变量,一定要在局部变量定义之前指向全局变量
28     printf("%d
",a);//局部变量
29     printf("%d
",*p);//全局变量
30     return 0;
31 }

二、结构体

结构体定义:

struct 结构体名

{
  类型名1 成员名1;
  类型名2 成员名2;
  ……
  类型名n 成员名n;   
};

 1 #include <stdio.h>
 2 //结构体
 3 
 4 //日期结构定义,不会分配存储空间
 5 //定义结构体1
 6 struct Date
 7 {
 8     int year;
 9     int month;
10     int day;
11 };
12 //定义结构体2
13 struct
14 {
15     //结构体的成员
16     int age;
17     double height;
18     char *name;
19 }s;//也可以省略结构体名
20 
21 
22 //定义结构体3
23 struct Person
24 {
25     //结构体的成员
26     int age;
27     double height;
28     char *name;
29     Date birthday;//结构体也可以包含别的结构体
30 }stu;//直接再定义结构体的同时定义结构体变量
31 
32 //结构体的作用域:从定义到代码块结束
33 
34 
35 
36 
37 
38 int main()
39 {
40     //写法1
41     struct Person p = {23,180.9,"送礼了"};
42     //写法2
43     struct Person p1 = {.name = "送礼了",.age = 25,.height = 182.4};
44     
45     //错误写法
46     //struct Person p2;
47     //p2 = {23,180.9,"送礼了"};
48     
49     //开始分配存储空间
50     struct Date d1 = {2015,4,13};
51     d1.year = 2000;//修改其中一个变量,用运算符.
52     struct Date d2 = d1;//结构体赋值,逐个赋值
53     
54     return 0;
55 }

三、结构体数组

    //数组定义和正常数组一样

    struct Birthday bir[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};

 1 #include <stdio.h>
 2 //结构体数组
 3 
 4 struct Birthday
 5 {
 6     int year;
 7     int month;
 8     int day;
 9 };
10 
11 int main()
12 {
13     //数组定义和正常数组一样,使用方法也是
14     struct Birthday bir[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};
15     
16     return 0;
17 }

四、结构体作为函数参数

  将结构体变量作为函数参数进行传递时,其实传递的是全部成员的值,也就是将实参中成员的值一一赋值给对应的形参成员。因此,形参的改变不会影响到实参。

 1 #include <stdio.h>
 2 //结构体数组
 3 
 4 struct Birthday
 5 {
 6     int year;
 7     int month;
 8     int day;
 9 };
10 
11 void AddMonth(struct Birthday bi);//函数声明,参数是结构体
12 int main()
13 {
14     //数组定义和正常数组一样
15     struct Birthday bi[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};
16     AddMonth(bi[0]);
17     printf("修改后:
%d-%d-%d
",bi[0].year,bi[0].month,bi[0].day);
18 
19     return 0;
20 }
21 
22 void AddMonth(struct Birthday bi)
23 {
24     printf("修改前:
%d-%d-%d
",bi.year,bi.month,bi.day);
25     bi.month = 5;
26 }

结果:

五、结构体的指针

 结构体指针变量的定义形式:struct 结构体名称 *指针变量名

结构体成员的访问方式:

  • 结构体变量名.成员名
  • (*指针变量名).成员名
  • 指针变量名->成员名
 1 #include <stdio.h>
 2 //结构体数组
 3 
 4 struct Birthday
 5 {
 6     int year;
 7     int month;
 8     int day;
 9 };
10 
11 void AddMonth(struct Birthday bi);//函数声明
12 int main()
13 {
14     //数组定义和正常数组一样
15     struct Birthday bi[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};
16     AddMonth(bi[0]);
17     printf("修改后:
%d-%d-%d
",bi[0].year,bi[0].month,bi[0].day);
18 
19     //结构体指针
20     struct Birthday *p;//定义
21     p = &bi[1];//赋值
22     
23     printf("%d-%d-%d
",p->year,(*p).month,p->day);//两种使用方式p->year,(*p).year
24 
25     return 0;
26 }
27 
28 void AddMonth(struct Birthday bi)
29 {
30     printf("修改前:
%d-%d-%d
",bi.year,bi.month,bi.day);
31     bi.month = 5;
32 }

 六、枚举

一般形式为:enum 枚举名 {枚举元素1,枚举元素2,……}; 

对固定值的使用更加方便,里面保存的是整形常量。默认从0开始依次递增。

  定义如下:

  //枚举定义

      enum week{Monday,Tuesday,Wednesday,T hursday,Friday,Saturday,Sunday};

   //使用

      enum week w;

      w = Tuesday;//赋值

 1 #include <stdio.h>
 2 
 3 
 4 int main()
 5 {
 6     //枚举定义
 7     enum week{Monday,Tuesday,Wensday,Thusday,Friday,Saturday,Sunday};
 8     
 9     //使用
10     enum week w;
11     w = Tuesday;//赋值
12     
13     printf("%u
",w);
14     
15     return 0;
16 }

数据类型总结:

基本数据类型:

char   // 1 %c,%d ASCII值

int

  short int ,short       //2 %d,%i

  long int ,long       //8 %ld

  unsigned int ,unsigned  //4 %zd

  signed int ,int ,signed  //4 %d,%i

float   // 4 %f

double  //8 %f

构造类型

  数组:只能是同一种类型的数据组成   int a[100];

  结构体:可以有多重数据类型组成     struct stu{int age;char *name;};

指针类型

  指针定义   int *p;

  间接操作变量:  int a = 10;p = &a;printf("%d",*p);

枚举类型

  使用场合:当一个变量的只允许有几个固定取值时可以用枚举实现。

2015-4-13 今日如此,明日依旧。

原文地址:https://www.cnblogs.com/songliquan/p/4423554.html