2-1.c数组指针的定义:
#include <stdio.h>
int main()
{
char str[]="China Beijing Fujian"; //定义数组str并进行初始化
char *pstr; //定义指针变量pstr
pstr=&str[6]; //初始化pstr,并使指针变量pstr指向数组str
printf("str:%s,str");
printf("pstr指向str[6]:%c
,*pstr");
return 0;
}
结果:
str:China Beijing Fujian pstr指向str[6]:B
2-2.c数组指针的引用
#include <stdio.h>
int main(void)
{
char str[]="China Beijing Fujian"; //定义数组str并进行初始化
char *pstr; //定义指针变量pstr
printf("str:%s
",str); //输出数组str,%s表示字符串
pstr=str; //初始化指针变量pstr,pstr指向数组str;pstr和str指向数组str首地址
printf("pstr->str:%s
",pstr); //以%s的形式输出pstr指向的数组元素
printf("&str=%p
",&str); //%p指针的值,输出数组名str的地址,即&str。连续数组空间的首地址,即str[0]的地址
printf("pstr=%p
",pstr); //输出指针变量pstr的值。因为pstr指向数组str,pstr的值就是str的地址,也是数组元素,str[0]的地址
printf("&str[0]=%p
",&str[0]);
return 0;
}
结果:
str:China Beijing Fujian pstr->str:China Beijing Fujian &str=0x7fffd12e1c80 pstr=0x7fffd12e1c80
2-3
#include <stdio.h>
int main(void)
{
char str[]="China Fujian";
char *pstr;
pstr=str;
while(*pstr)
{
printf("%c:%p
",*pstr,pstr); //%c单个字符,第一次执行时,pstr指向str[0],输出str[0]以及&str[0]
pstr++; //让指针指向下一个数组元素
}
return 0;
}
结果:
C:0x7fff13c1f1b0 h:0x7fff13c1f1b1 i:0x7fff13c1f1b2 n:0x7fff13c1f1b3 a:0x7fff13c1f1b4 :0x7fff13c1f1b5 F:0x7fff13c1f1b6 u:0x7fff13c1f1b7 j:0x7fff13c1f1b8 i:0x7fff13c1f1b9 a:0x7fff13c1f1ba n:0x7fff13c1f1bb
2-4.c数组的下标表示法
#include <stdio.h>
#define WEEKNUM 7 //定义一个宏
int main(void)
{
int temp;
int week[WEEKNUM]={1,2,3,4,5,6,7};
printf("please input today is:");
scanf("%d",&temp);
if(temp<=WEEKNUM)
{
printf("tomorrow is :%d
",week[temp]); //%d十进制有符号字符
}
else
{
printf("Error
");
}
return 0;
}
结果:
please input today is:4 tomorrow i
2-5
#include <stdio.h>
#define LEN 8
int main(void)
{
char i;
char str[LEN]="Fujian";
printf("str:%s
",str);
for(i=0;i<LEN;i++)
{
printf("%c:%p
",str[i],&str[i]);
}
return 0;
}
结果:
str:Fujian F:0x7fff3f5c5d20 u:0x7fff3f5c5d21 j:0x7fff3f5c5d22 i:0x7fff3f5c5d23 a:0x7fff3f5c5d24 n:0x7fff3f5c5d25 :0x7fff3f5c5d26 :0x7fff3f5c5d27
2-6.c数组的指针表示法
#include <stdio.h>
#define LEN 10
int main(void)
{
char str[LEN]={'A','B','C','D','E','F','G','H','I','J'};
char idx,*pstr;
pstr=str;
printf("please input (0-9)and ENTER:
");
scanf("%d",&idx);
if(idx<LEN)
{
printf("The character is:%c
",*(pstr+idx)); //patr指向数组str,即指向数组的第一个元素str[0],当运行pstr+idx运算后,,指针pstr指向str[idx],*(patr+idx)就是数组元素str[idx]
}
else
{
printf("The idx is overflow
");
}
return 0;
结果:
please input (0-9)and ENTER: 4 The character is:E
2-8.c数组的下标法和指针
#include <stdio.h>
#define LEN 15
int main(void)
{
char str[LEN]="Fujian.2018";
char idx,*pstr;
for(idx=0;idx<LEN;idx++)
{
printf("%c",str[idx]); //下标法
}
printf("
");
pstr=str;
while(*pstr)
{
printf("%c",*(pstr+idx)); //指针法
pstr++;
}
printf("
");
return 0;
}
结果:
Fujian.2018
2-9.c指针数组的引用
#include <stdio.h>
int main(void)
{
char *str[]={"Fujian","Huian"}; //定义数组str并初始化
printf("str[0]:%s
",str[0]); //%s一次性输出一个字符串,若要一次性输出一个字符串需要知道字符串的首地址,str[0]即为首地址
printf("str[1]:%s
",str[1]);
return 0;
}
结果:
str[0]:Fujian str[1]:Huian
2-10
#include<stdio.h>
int main()
{
char temp;
char *str[]={"IllgalDay","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
printf("please input number:
");
scanf("%d",&temp);
if(temp<7)
{
switch(temp)
{
case 0:
printf("Ingall day
");
break;
case 1:
printf("Monday
");
break;
case 2:
printf("Tuesday
");
break;
}
}
return 0;
}
结果:
please input number: 1 Monday
2-11.c字符串指针的引用
#include <stdio.h>
int main(void)
{
char *str="Fujian 2018"; //相当于 char *str; str ="Fujian 2018";
printf("%s
",str); //指针变量str指向字符串的“Fujian 2018”的首地址,这里仅仅是指向不是赋值
return 0;
}
结果:
Fujian 2018
2-12数组方式实现
#include <stdio.h>
int main(void)
{
char str[]="Fujian 2018"; //定义数组,并进行初始化
printf("%s
",str);
return 0;
}
结果:
Fujian 2018
利用数组形式定义字符串就需要知道字符串大小,而指针形式则没有这个限制
2-13
#include <stdio.h>
int main()
{
char *str="Fujian 2018";
printf("%s
",str); //%s字符串
printf("%p
",str); //%p指针,输出str,字符串首地址
while(*str)
{
printf("%c",*str); //输出字符
printf(" %p
",str); //输出字符地址
str++;
}
return 0;
}
结果:
Fujian 2018 0x4006c8 F 0x4006c8 u 0x4006c9 j 0x4006ca i 0x4006cb a 0x4006cc n 0x4006cd 0x4006ce 2 0x4006cf 0 0x4006d0 1 0x4006d1 8 0x4006d2
一步一个脚印......