黑马程序员--C语言之复杂数据类型

来源:互联网 发布:买大班模型 淘宝店 编辑:程序博客网 时间:2024/04/29 05:46

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

C语言的学习过程大致上包括基本语法、基本运算、流程控制等。本篇博客先总结下C语言的复杂数据类型。

使用工具:Xcode5.1.1



复杂数据类型主要包括枚举、数组、字符串和指针等

枚举

枚举的基本形式:enum 枚举名 {枚举值表(即枚举元素)}

枚举定义变量的方式:

方式一:

enum score { score , avgScore , sumScore};

enum score a , b , c ;

方式二:

enum score { score , avgScore , sumScore}a,b,c;

方式三:

enum { score , avgScore , sumScore}a,b,c;


枚举使用注意:

1、枚举值是常量,不能是变量

2、枚举元素本身由系统定义了一个表示序号的值:0、1、2.。。。。。可以这样访问

例:a = (enum score)1;//相当于 a = avgScore ;

3、枚举元素不是字符常量也不是字符串常量,用的时候不能加单、双引号


数组

数组的基本形式:数组类型  数组名[ 数组长度]={元素},如:int age[4]={100,68,88,95,59}

例:3个班各班学员人数

#include <stdio.h>int main(){int everyClassCount [ 3 ]= { 36 , 40 , 33};for(int i = 0;i<sizeof(everyClassCount)/sizeof(int);i++){printf("%d斑有%d人",i+1,everyClassCount[i]);}return 0;}


数组使用注意:

1、数组的内存存储:按照元素下标由小到大顺序。

2、数组的元素长度为变量时一般不能同时初始化;如果用到变量并进行初始化,可以用宏(#define)来处理。

例:

#include <stdio.h>#define COUNT=6int main(){int num[COUNT] = {15,20,65,95,15,23};//在未定义宏时此用法错误,但定义宏后可使用printf("%d",num[5]);return 0;}

字符串

C语言中字符串存储需用数组。

例:打印hello,并区别字符串和字符数组

#include <stdio.h>int main(){//字符串会以"\0"结束。char word1[] = "hello";//字符串char word[] = {'h','e','l','l','o'};//字符数组char word3[] = {'h','e','l','l','o','\0'};//字符串printf("%s",word1);printf("%s",word2);//word2是字符数组,强制以字符串打印会打印后面的字符或字符串(知道遇见"\0"为止)return 0;}

字符串使用注意:

1、char name1 [] ="Jerry"和 char name2 [] ={'T','o','m'},第一个是字符串,第二个是字符数组,字符串会以"\0"结束。

2、strlen用来计算除“\0”以外字符串的长度。


指针

指针的基本形式: 变量类型 *变量名

#include <stdio.h>int main(){//指针指向的是存储位置,因此要在指向的变量前面加上&int a = 10;int *p= &a;//可以这样使用,但是int *p;*p=&a;此用法错误*p = 30;printf("%d",a);//由于指针p指向a,所以p修改,a的值也会改变return 0;}


指针使用注意:

1、指针变量只能存储地址,所以在赋值时会以*p=100 等。

2、int *num=&a;//正确用法

int *num;

*num=&a;//错误用法,&a、num本来就是存储地址,*num代表指向num地址的地址。

3、"*"在定义和使用时作用大不相同:定义时int *相当于一个数据类型;在使用时,是访问p所在的存储空间。

4、指针用在数组时,指向数组的首元素,指针可以像数组一样访问元素。

例:

#include <stdio.h>int main(){int count[]={99,55,33,22};int *p=&count;printf("%d\n",p[2]);printf("%d\n",p+2);return 0;}

结构体

结构体是不同数据类型的集合。很多地方类似于数组。

结构体的形式:

struct 结构体名

{

结构体元素

};

例:

#include <stdio.h>int main(){struct Person{int age ;char name[] ;double weight ;};//分号不可省struct Person p1 = { 10,"Jack",45.2};struct Person p2 = {.weight=60.5,.name="Lucy",.age=22};printf("%s,\ %s",p1.name,p2.name);printf("%d,\ %d",p1.age,p2.age);printf("%f,\ %f",p1.weight,p2.weight);return 0;}

结构体变量定义和赋值方式:

方式一:

struct Person

{

int age;

char name[];

double weight;

};


struct Person p1 = {33,"Tom",100};

方式二:

struct Person

{

int age;

char name[];

double weight;

}p2 = {22,"Jerry",65.5};

方式三:

struct {

int age;

char name[];

double weight;

}p3 = {20,"Angle",55.5};


和数组类似,结构体也可以嵌套:

例:

#include <stdio.h>int main(){struct Date{int year;int month;int day;};struct Person{int age;struct Date birthday;//嵌套structchar *name;};struct Person p = { 20,{1990,10,11},"Moly"};printf("%d\n",p.age);print("%d,%d,%d\n",p.birthday.year,p.birthday.month,p.birthday.day);printf("%f\n",p.weight)return 0;}


预处理指令:

C语言预处理指令是编译过程中一个单独的步骤。所有的预处理指令都用"#"开头。常用预处理指令可分为:宏定义、条件编译、文件包含

1、宏定义:#define

例:见数组例。


宏定义使用注意:

1、例如:

#define NUM 10int main(){char *s = "NUMBER";printf("%s\n",s);return 0;}
双引号中NUM不会被替换

2、宏和函数的区别:

宏定义在编译之前进行(效率比函数高);函数在打开程序时才调用。

3、宏定义不检测类型


2、条件编译。

#if 条件1

代码1

#elif 条件2

代码2

#else

代码3

#endif
形式虽然和if-else类似,但是在运行时所有的if-else语句都要编译,而条件编译是满足条件再编译。

例:

#include <stdio.h>#define COUNT 5#if COUNT>5printf("COUNT大于5");#elif COUNT<5printf("COUNT小于5");#elseprintf("COUNT等于5");#endifint main(){int num[COUNT]={10,11,12,13,14};for(int i=0;i<count;i++){printf("%d\n",num[i]);}return 0;}


条件编译使用扩展:

检测是否定义宏:

 #if defined(NUM)//#ifdef NUM等同

     printf("已定义");

     #endif

     

     #if !defined(NUM)//#ifndef NUM等同

     printf("未定义");

     #endif

3、文件包含。

#include <stdio.h>//用尖括号是包含系统文件

#include "program1.h"//引号是优先包含所在目录的文件,如果所在目录没有,在系统目录找

文件包含使用注意:

test1.h文件

//test1.h#include <stdio.h>void test1(){printf("This is test1");}

test2.h文件

//test2.h#include <stdio.h>#include "test1.h"void test2(){printf("This is test2");}

test.c文件

//test.c#include <stdio.h>#include "test1.h"#include "test2.h"int main(){test1();test2();return 0;}

这样写是错误的test2中已调用test1,应该改进test1和test2

test1.h文件

#include <stdio.h>#ifndef _TEST1_#define _TEST1_void test1(){printf("This is test1");}#endif


test2.h文件

#include <stdio.h>#ifndef _TEST1_#define _TEST1_#include "test1.h"void test2(){printf("This is test2");}#endif


0 0