指针数组的应用场景

1.做菜单:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// 求关键字在表中的位置
// 一个入口,多个出口
void searchKeyTable(const char * table[], const int size, const char *key, int *pos)
{
	// 已经退化为指针了  打印的结果都是4
	printf("sizeof(table) : %d \n", sizeof(table));  // 4
	printf("sizeof(*table) : %d \n", sizeof(*table));  // 4


	int rv = 0;
	int i = 0;
	int inum = 0;
	if (table == NULL || key == NULL || pos == NULL)
	{
		rv = -1;
		printf("func searchKeyTable : %d \n", rv);
		return rv;
	}

	for (i = 0; i < size; i++)
	{
		if (strcmp(key, table[i]) == 0)
		{
			*pos = i;
			return rv;
		}
	}

	if (i == size)
	{
		*pos = -1;
	}
	return rv;
}

#define DIM(a) (sizeof(a) / sizeof(*a))

void main()
{
	int inum = 0;
	int pos = 0;
	int a[10];
	int i = 0;
	// 指针数组
	char *c_keyword[] = {
		"while",
		"case",
		"static",
		"do"
	};

	printf("%d\n", sizeof(c_keyword));   // 16
	printf("%d \n", sizeof(*c_keyword));  // 4 

	searchKeyTable(c_keyword, DIM(c_keyword), "static", &pos);

	printf("pos : %d", pos);

	system("pause");
	return;
}

2.命令行:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 作命令行
/*
	指针都是浮云,最重要的是看内存块是谁给你产生的
	这才是问题的关键,关键要看内存空间是谁给你分配的
	是在什么地方分配的
	操作系统会提前分配内存
*/
int main(int argc, char *argv[], char **env)
{
	int i = 0;
	printf("================Begin argv====================\n");
	for (i = 0; i < argc; i++)
	{
		printf("%s\n", argv[i]);
	}
	printf("================End argv====================\n");

	printf("\n");
	printf("\n");
	printf("\n");

	printf("================Begin env====================\n");
	for (i = 0; env[i] != NULL; i++)
	{
		printf("%s \n", env[i]);
	}
	printf("================End env====================\n");
	getchar();
	// system("pause");
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值