1.通过数组下标所能完成的任何操作都可以通过指针来实现。一般来说,用指针编写的程序比用数组下标编写的程序执行数度快。例如:
对数组元素a[i]的引用,C语言实际上是现将其转换为*(a+i)的形式,再进行求值。
2.字符串常量作为函数参数使用,实际上是通过字符指针访问该字符串的。例如:
printf("hello world\n");
在上述语句中,字符串常量是一个字符数组,printf接受的是一个指向字符数组第一个字符的指针。
3.指针数组,例如:
char *menu[7]={"bin convert into decimal","bin convert into hex","decimal convert into bin","decimal convert into hex","hex convert into bin","hex convert into decimal","exit system"};
她表示menu是一个具有7个元素的一位数组,其中数组的每个元素是一个指向字符对象的指针。也就是说menu[i]是一个字符指针,而*menu[i]是该指针指向的第i个问本行的首字符。注意menu是数组名,常量,不能对她进行类似menu++的变量的操作。
附:菜单界面实例
//实现二、十、十六进制的转化
#include<stdio.h>
#include<stdlib.h>//include system() function
int main(){
system("title System Convert");
int choice=0;
int flag=1;//indicate exitting system
char *menu[7]={"bin convert into decimal","bin convert into hex","decimal convert into bin",
"decimal convert into hex","hex convert into bin","hex convert into decimal","exit system"};
while(flag){//if flag equal 0,exit system
printf(" system convert \n");
printf("*************************\n");
for(int i=0;i<7;i++)
printf("%d.%s\n",i+1,menu[i]);//printf("%d.%s\n",i+1,*(menu+i));
printf("Please input the number of your choice:\n");
scanf("%d",&choice);
switch(choice){
case 1:printf("bin_decimal()\n");
system("pause");
system("cls");
break;
case 2:printf("bin_hex\n");
system("pause");
system("cls");
break;
case 3:printf("decimal_bin\n");
system("pause");
system("cls");
break;
case 4:printf("decimal_hex\n");
system("pause");
system("cls");
break;
case 5:printf("hex_bin\n");
system("pause");
system("cls");
break;
case 6:printf("hex_decimal\n");
system("pause");
system("cls");
break;
case 7:flag=0;
system("cls");
break;
default:system("cls");
printf("input error:please input the number of your choice again:\n");
}
}
return 0;
}
}