C语言 数组学习

数组

数组 即 数据类型相同 大小确定 且 存放在连续内存地址的 数据

数组是占内存的,并且操作系统会给你随机分配内存地址编号

地址:实际上是你内存条上的东西,虚拟出来的一个地址 用来表示数据在内存条的那个位置 使用数据的16进制的形式表示

一维数组

一维数组声明方式:

type arrayName [ arraySize ];
type arrayName [ ]={date1,data2,data3,...};

arraySize 必须是一个大于零的整数常量,type 可以是任意有效的 C 数据类型。例如,要声明一个类型为 double 的包含 10 个元素的数组 balance

声明语句如下:

//声明一个空数组
double balance[10];
//声明一个数组并初始化 也可以用循环来初始化
double balance[10]={10,9,8,7,6,5,4,3,2,1};
//声明一个数组并初始化一部分 其余未初始化的部分为0
double balance[10]={10,9};
//声明一个数组并初始化 其大小为初始化数据的个数
double balance[]={10,9,8,7,6,5,4,3,2,1};

现在 balance 是一个可用的数组,可以容纳 10 个类型为 double 的数字。

一位数组的访问方式

arrayName [ subscript ];

arrayName为数组名,subscript为数据下标 第一个值为0 最后一个值为数组长度减一 超过数组最大长度时就会报错

例子: 将balance数组中第二个值放入 变量a中

double balance[10]={10,9,8,7,6,5,4,3,2,1};
double a =balance[2-1]

打印数组的内存地址:

    int balance[10] = { 10,9,8,7,6,5,4,3,2,1 };

    for (int i = 0;i < 10;i++) {

        printf("%p\r\n", &balance[i]);

    }

结果:

image-20221015141251695

求一维数据的大小

sizeof(arr)/sizeof[0] 只能求到数组的最大个数不能求到 其中具体有用的值

int arr[] ={1,3,5,6,78,6,8,6,78};
//使用sizeof(arr)得到总数组大小 除以 单个数据类型的大小得到 元素个数
printf("%d",sizeof(arr)/sizeof[0]);

二维数组

二维数组的声明方式

type name[size1][size2]...[sizeN];

例子: 声明创建了一个三维 5 . 10 . 4 整型数组:

int threedim[5][10][4];

多维数组最简单的形式是二维数组。一个二维数组,在本质上,是一个一维数组的列表。声明一个 x 行 y 列的二维整型数组,形式如下:

type arrayName [ x ][ y ];

其中,type 可以是任意有效的 C 数据类型,arrayName 是一个有效的 C 标识符。一个二维数组可以被认为是一个带有 x 行和 y 列的表格。下面是一个二维数组,包含 3 行和 4 列:

int x[3][4];

初始化二维数组

多维数组可以通过在括号内为每行指定值来进行初始化。下面是一个带有 3 行 4 列的数组。

int a[3][4] = {  
 {0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
 {4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
 {8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
};

内部嵌套的括号是可选的,下面的初始化与上面是等同的:

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

二维数组的访问方法

int val = a[2][3];

二维数组推箱子

//标准输入输出库
#include <stdio.h>
#include <stdlib.h>
//用于传递命令给提示符窗口
#include <Windows.h>
//用于获取键盘码
#include <conio.h>
int game_map[10][10];
int map_raw_size = sizeof(game_map) / sizeof(game_map[0]); //行大小
int map_col_size = sizeof(game_map[0]) / sizeof(game_map[0][0]);//列大小
//玩家x坐标
int player_position_x;
//玩家y坐标
int player_position_y;
//以下方法名可以放在头文件里

int get_rand_num(int seed, int max_num);
int create_palyer();
int get_rand_num(int seed, int max_num);
int create_box();
//生成地图四周的墙壁
void init_game_map() {

	{ //生成墙
		//行
		for (size_t i = 0; i < map_raw_size; i++)
		{
			//列
			for (size_t j = 0; j < map_col_size; j++)
			{
				if (i == 0 || i == map_raw_size - 1 || j == 0 || j == map_col_size - 1)
				{
					game_map[i][j] = 1;
				}
			}
		}
	}
	
	while (create_palyer());
	while (create_box());
	while (create_box());
	
}

//创建盒子  返回0为创建成功
int create_box() {
	
	int box_position_x = get_rand_num((unsigned)time(NULL), map_raw_size - 1);
	int box_position_y = get_rand_num(box_position_x, map_col_size - 1);

	if (game_map[box_position_y][box_position_x] == 0
		&& game_map[box_position_y + 1][box_position_x] !=1
		&& game_map[box_position_y - 1][box_position_x] != 1
		&& game_map[box_position_y][box_position_x - 1] != 1
		&& game_map[box_position_y][box_position_x + 1] != 1
		) {
		game_map[box_position_y][box_position_x] = 3;
		return 0;
	}
	else {
		return 1;
	}
}
//创建盒玩家  返回0为创建成功
int  create_palyer() {//生成玩家

	player_position_x = get_rand_num((unsigned)time(NULL), map_raw_size - 1);
	player_position_y = get_rand_num(player_position_x, map_col_size - 1);
	if (game_map[player_position_y][player_position_x] == 0) {
		game_map[player_position_y][player_position_x] = 2;
		return 0;
	}
	else {
		return 1;
	}

	printf("x:%d y:%d\r\n", player_position_x, player_position_y);
}
//获得随机数 seed 为种子 max_num为最大值
int get_rand_num(int seed, int max_num) {

	srand((unsigned)(time(NULL) + seed));

	return	rand() % max_num + 1;
}

//打印二维数组
void print_game_map() {

	for (size_t i = 0; i < map_raw_size; i++)
	{
		//列
		for (size_t j = 0; j < map_col_size; j++)
		{
			printf("%d ", game_map[i][j]);
		}
		printf("\r\n");
	}
}
//移动玩家
void player_move(int aspect) {

	switch (aspect)
	{
		//键盘上键
	case 72:
		if (game_map[player_position_y - 1][player_position_x] == 0)
		{
			//判断玩家即将移动的方向是否有阻碍
			game_map[player_position_y][player_position_x] = 0;
			game_map[player_position_y -= 1][player_position_x] = 2;
		}
		else if (game_map[player_position_y - 1][player_position_x] == 3) {
			//判断玩家即将推动箱子的方向是否有阻碍
			if (game_map[player_position_y - 2][player_position_x] == 0)
			{
				game_map[player_position_y][player_position_x] = 0;
				game_map[player_position_y - 2][player_position_x] = 3;
				game_map[player_position_y -= 1][player_position_x] = 2;

			}
		}
		printf("U\r\n");
		break;
		//键盘左键
	case 75:
		printf("L\r\n");
		if (game_map[player_position_y][player_position_x - 1] == 0)
		{
			game_map[player_position_y][player_position_x] = 0;
			game_map[player_position_y][player_position_x -= 1] = 2;
		}
		else if (game_map[player_position_y][player_position_x - 1] == 3) {
			if (game_map[player_position_y][player_position_x - 2] == 0)
			{
				game_map[player_position_y][player_position_x] = 0;
				game_map[player_position_y][player_position_x - 2] = 3;
				game_map[player_position_y][player_position_x -= 1] = 2;

			}
		}
		break;
	case 80:
		//键盘下键
		printf("D\r\n");
		if (game_map[player_position_y + 1][player_position_x] == 0)
		{
			game_map[player_position_y][player_position_x] = 0;
			game_map[player_position_y += 1][player_position_x] = 2;
		}
		else if (game_map[player_position_y + 1][player_position_x] == 3) {
			if (game_map[player_position_y + 2][player_position_x] == 0)
			{
				game_map[player_position_y][player_position_x] = 0;
				game_map[player_position_y + 2][player_position_x] = 3;
				game_map[player_position_y += 1][player_position_x] = 2;

			}
		}
		break;
		//键盘右键
	case 77:
		printf("R\r\n");
		if (game_map[player_position_y][player_position_x + 1] == 0)
		{
			game_map[player_position_y][player_position_x] = 0;
			game_map[player_position_y][player_position_x += 1] = 2;
		}
		else if (game_map[player_position_y][player_position_x + 1] == 3) 
		{
			if (game_map[player_position_y][player_position_x + 2] == 0)
			{
				game_map[player_position_y][player_position_x] = 0;
				game_map[player_position_y][player_position_x + 2] = 3;
				game_map[player_position_y][player_position_x += 1] = 2;
			}
		}
		break;
	default:
		break;
	}
}


int main(void) {
	init_game_map();
		/*
   L:75
   D:80
   R:77
   U:72
*/
		while (1)
		{
			//windows 命令提示符窗口清屏
			system("cls");
			//修改提示符的前景色为蓝色
			system("color b");
			//打印数组
			print_game_map();
			//传递键盘输入的值
			player_move(_getch());
		}
	return 0;
}
结果:

image-20221001211427812

字符数组

字符数组的声明方式

char 数组也可以为多维数组

char 可以存储中文 但是一个中文字符占据两个char大小 但是总大小为 汉字数*2+1 因为还有一个’\0’结尾符号

在windows中会把空格当作’\0’ 所以在printf输出空格时 后面的字符将不会被输出

!没有初始化的部分 用 '\0’表示 其意义为字符结尾 当printf检测到 \0时就不会继续输出后方的字符

//可以和数字数组的方式一样
char str[10]={'a','a','b'};
	 str[3]='c';
//也可以使用双引号进制初始化 可以省略{} 也可以加上
char str2[] = "abcdefh";

输出字符串数组

可以通过常规的for循环进行遍历输出

也可以使用printf使用%s对字符串进行输出

char str[10]={'a','a','b'};
char str2[10] = "abcdefh";
for (size_t i = 0; i < 10; i++)
{
	printf("%c\r\n", str[i]);
}
printf("%s", str2);

通过scanf_s给字符串数组赋值

scanf_s("%s", str3,10); 10代表的是你即将要输入char数组的大小 由于数组默认为地址传递 所以str3可以不用加&

#include <stdio.h>
int main()
{
	char str3[10];
	scanf_s("%s", str3,10);
	printf("%s", str3);
	return 0;
}

结果

image-20221003180332429

通过gets输入字符串 通过puts输出字符串

#include <stdio.h>
int main()
{
    char str4[10];
    gets(str4);
    puts(str4);
	return 0;
}

结果

image-20221003181306531

strcmp()

strcmp(s1, s2);

需要导入string.h库

会从左往右比较 如果第一个字符串左边出现的字符ASCII码大于第二个字符串 那么 比较停止 返回 >0的值

  1. 如果 s1 和 s2 是相同的,则返回 0

  2. 如果 s1<s2 则返回小于 0

  3. 如果 s1>s2 则返回大于 0

#include <stdio.h>
#include <string.h>
int main()
{
    char str5[]="aaaa";
    char str6[] = "zx";
    char str7[] = "aa";
    char str8[] = "aaaa";
    printf("str5 str6: %d\n", strcmp(str5, str6));
    printf("str5 str7: %d\n", strcmp(str5, str7));
    printf("str5 str8: %d\n", strcmp(str5, str8));
	return 0;
}

结果

image-20221003182511780

strlen()

strlen(s1)

需要导入string.h库

strlen是求字符串有效大小 即 ‘\0’ 不会被计算

注意strlen与sizeof 不同 sizeof是求类型长度 而strlen是求字符串有效长度

strcat()

strcat(s1, s2);

需要导入string.h库

连接字符串 s2 到字符串 s1 的末尾。

注意字符串s1 必须要有足够的控件存放s2不然会 报错

例子:

char str9[20]="adcc";
char str10[] = "efgcdef";
strcat(str9, str10);
printf("%s  %s", str9, str10);	

结果:

image-20221003191726611

strcpy()

**strcpy(s1, s2) ; ** 复制字符串 s2 到字符串 s1。直接覆盖

例子:

char str9[20]="adcc";
char str10[] = "efgcdef";
strcpy(str9, str10);
printf("%s", str9, str10);

结果:

求字符串有效长度

strcat()

strcat(s1, s2);

需要导入string.h库

连接字符串 s2 到字符串 s1 的末尾。

注意字符串s1 必须要有足够的控件存放s2不然会 报错

例子:

char str9[20]="adcc";
char str10[] = "efgcdef";
strcat(str9, str10);
printf("%s  %s", str9, str10);	

结果:

[外链图片转存中…(img-P6V6QWZz-1665638085690)]

strcpy()

**strcpy(s1, s2) ; ** 复制字符串 s2 到字符串 s1。直接覆盖

例子:

char str9[20]="adcc";
char str10[] = "efgcdef";
strcpy(str9, str10);
printf("%s", str9, str10);

结果:

image-20221003191758891

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值