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;
}