您的位置:首页 > 其它

数组

2015-07-17 00:02 169 查看
数组是同类型数据的结合,同一个数组的数组元素具有相同的数据类型,引用数组就是引用数组的各元素,通过下标的变化可以引用任意一个数组元素,需要注意的是,不要进行下标越界的引用,那样会带来意外的副作用。

数组的理解:

#include<stdio.h>

int main(void) {

int a[2];//在a处开辟两个4个字节大小的内存空间

a[0] = 1;//在a处偏移0个位置赋值为1

a[1] = 2;//在a处偏移1个位置赋值为2

printf("%d\n", a);//a处存的东西

printf("%d\n", &a[0]);//【a存的地址】处【再偏移0】处的【地址】

printf("%d\n", a[0]);//【a处的地址】处【再偏移0】处的【值】

printf("%d\n", &a[1]);//【a存的地址】处【再偏移1】处的【地址】

C语言中用字符数组来存放字符串,该字符数组中包含一个"\0"字符,代表字符串结尾

字符串数组:

#include<stdio.h>

#include<string.h>

int main(void) {

// int a[2][2][3] = {{{1, 2 ,3}, {1, 2, 3}}, {{1, 2, 3}, {1, 2, 3}}};

char b[] = "china";

int a[] = {1, 2, 3};

char c = 65;

//%d c可输出为数字

//%c c也可输出为字符

printf("%s\n", b);// 如果按%s字符串格式输出,则为字符串

printf("%d\n", b);//如果首地址按%d整形输出,则为地址

printf("%d\n", a);

char national[2][100] = {"China", "England"};

//数组再存数组,便是二位数组

int grade[2][2] = {{99, 100}, {88, 66}};

//数组存数组再存一个数组,便是三维数组(字符串是数组)

char users[2][2][8] = {{"xiaosan", "123"},{"xiaosi", "123"}};

char p[] = "123";

char pp[] = "123";

char ppp[] = "1234";

printf("%d\n", p == pp);//p和pp是首地址,两个数组的首地址不一样

printf("%d\n", strcmp(p, pp) == 0);//strcmp是比较的两个字符串(经过处理的,不再是地址类似于%s)

printf("%d\n", strcmp(p, ppp) == 0);

}

复杂的数据结构:

一、基本类型:

1、整数型(int)

2、浮点型

①单精度型(float)

②双精度型(double)

3、字符型(char)

二、指针类型

三、构造类型

1、数组 2、结构型(struct)
3、联合型(union) 4、枚举型(enum)

四、空类型(void)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: