您的位置:首页 > 理论基础 > 数据结构算法

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

2015-04-13 23:15 302 查看
目录:

[b]1、全局变量和局部变量[/b]
[b]2、结构体[/b]

[b]3、结构体数组[/b]

[b]4、结构体做函数参数[/b]

[b]5、结构体指针[/b]

[b]6、枚举[/b]

[b]7、总结[/b]

一、全局变量和局部变量

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

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

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

    默认初始值是0

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

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

    没有默认初始值

代码示例

#include <stdio.h>

/*
全局变量和局部变量
*/

//全局变量,再函数外定义的变量
//作用范围:是从变量定义到文件结束
//默认初始值是0

//局部变量,再函数内部定义的变量
//作用域:从变量定义开始到函数结束
//没有默认初始值

//全局变量
int a = 10;

int main()
{
int *p = &a;//使用局部指针指向全局变量
//局部变量
int a = 20;

a++;//这样使用,引用的时局部变量,再函数内部定义的局部变量如果和全局变量重名,那么局部变量会覆盖全局变量,如果要在函数内部使用全局变量,可以使用局部指针指向全局变量,一定要在局部变量定义之前指向全局变量
printf("%d\n",a);//局部变量
printf("%d\n",*p);//全局变量
return 0;
}


二、结构体

结构体定义:

struct 结构体名

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

#include <stdio.h>
//结构体

//日期结构定义,不会分配存储空间
//定义结构体1
struct Date
{
int year;
int month;
int day;
};
//定义结构体2
struct
{
//结构体的成员
int age;
double height;
char *name;
}s;//也可以省略结构体名

//定义结构体3
struct Person
{
//结构体的成员
int age;
double height;
char *name;
Date birthday;//结构体也可以包含别的结构体
}stu;//直接再定义结构体的同时定义结构体变量

//结构体的作用域:从定义到代码块结束

int main()
{
//写法1
struct Person p = {23,180.9,"送礼了"};
//写法2
struct Person p1 = {.name = "送礼了",.age = 25,.height = 182.4};

//错误写法
//struct Person p2;
//p2 = {23,180.9,"送礼了"};

//开始分配存储空间
struct Date d1 = {2015,4,13};
d1.year = 2000;//修改其中一个变量,用运算符.
struct Date d2 = d1;//结构体赋值,逐个赋值

return 0;
}


三、结构体数组

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

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

#include <stdio.h>
//结构体数组

struct Birthday
{
int year;
int month;
int day;
};

int main()
{
//数组定义和正常数组一样,使用方法也是
struct Birthday bir[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};

return 0;
}


四、结构体作为函数参数

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

#include <stdio.h>
//结构体数组

struct Birthday
{
int year;
int month;
int day;
};

void AddMonth(struct Birthday bi);//函数声明,参数是结构体
int main()
{
//数组定义和正常数组一样
struct Birthday bi[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};
AddMonth(bi[0]);
printf("修改后:\n%d-%d-%d\n",bi[0].year,bi[0].month,bi[0].day);

return 0;
}

void AddMonth(struct Birthday bi)
{
printf("修改前:\n%d-%d-%d\n",bi.year,bi.month,bi.day);
bi.month = 5;
}


结果:



五、结构体的指针

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

结构体成员的访问方式:

结构体变量名.成员名

(*指针变量名).成员名

指针变量名->成员名

#include <stdio.h>
//结构体数组

struct Birthday
{
int year;
int month;
int day;
};

void AddMonth(struct Birthday bi);//函数声明
int main()
{
//数组定义和正常数组一样
struct Birthday bi[3] = {{1990,2,4},{2004,5,7},{2015,4,9}};
AddMonth(bi[0]);
printf("修改后:\n%d-%d-%d\n",bi[0].year,bi[0].month,bi[0].day);

//结构体指针
struct Birthday *p;//定义
p = &bi[1];//赋值

printf("%d-%d-%d\n",p->year,(*p).month,p->day);//两种使用方式p->year,(*p).year

return 0;
}

void AddMonth(struct Birthday bi)
{
printf("修改前:\n%d-%d-%d\n",bi.year,bi.month,bi.day);
bi.month = 5;
}


六、枚举

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

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

  定义如下:

  //枚举定义

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

  //使用

   enum week w;

   w = Tuesday;//赋值

#include <stdio.h>

int main()
{
//枚举定义
enum week{Monday,Tuesday,Wensday,Thusday,Friday,Saturday,Sunday};

//使用
enum week w;
w = Tuesday;//赋值

printf("%u\n",w);

return 0;
}


数据类型总结:

基本数据类型:

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 今日如此,明日依旧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: