//数组与指针
/*
#include<stdio.h>
void main ()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int *p;
int (*q)[10];
p = arr; //数组首元素地址
q = &arr; //数组首地址
//(数组首元素地址的操作)数组元素的引用与表示, 以第0个元素为例
printf("************数组元素的7种基本表示方法*****\n");
printf("----- arr[0] = %d, 0[arr] = %d, *(arr+0) = %d-----\n", arr[0], 0[arr], *(arr+0));
printf("----- p [0] = %d, 0[p] = %d, *(p+0) = %d-----\n", p[0], 0[p], *(p+0));
printf("--- *(int*)q = %d---------------------------------\n", *((int*)q+0));
printf("数组首元素的地址 = %p\n", p);
printf("数组首元素的地址加一后的地址及其值: %p, %d\n", p+1, *(p+1));
//(数组首地址的操作)指针指向的特殊改变
printf("数组首地址 = %p\n", q);
printf("数组首地址加一后的地址及其值:%p, %d\n", q+1, *(int*)(q+1));
}
*/
//指针数组---指针数组的定义初始化和使用
/*
#include<stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
char* arrc[3] = {"aaa", "bbb", "ccc"};
char** parrc = arrc;
int* arri[3] = {&a, &b, &c};
int** parri = arri;
//int* arri[] 地址操作&元素操作
//int* arri[] 地址操作
printf("arri[0] = %p, arri[1] = %p, arri[2] = %p\n", arri[0], arri[1], arri[2]);
printf("arri[0]+1 = %p, arri[1]+1 = %p, arri[2]-2 = %p\n", arri[0]+1, arri[1]+1, arri[2]-2);
printf("*(parri+0) = %p, *(parri+1) = %p, *(parri+2) = %p\n", *(parri+0), *(parri+1), *(parri+2));
//int* arri[] 元素操作
printf("*arri[0] = %d, *arri[1] = %d, *arri[2] = %d\n", *arri[0], *arri[1], *arri[2]);
printf("*(arri[0]+1) = %d, *(arri[1]+1) = %d, *(arri[2]-2) = %d\n", *(arri[0]+1), *(arri[1]+1), *(arri[2]-2));
printf("**(parri+0) = %d, **(parri+1) = %d, **(parri+2) = %d\n", **(parri+0), **(parri+1), **(parri+2));
//char* arrc[] 地址操作&元素操作
//char* arrc[] 地址操作
printf("arrc[0] = %p, arrc[1] = %p, arrc[2] = %p\n", arrc[0], arrc[1], arrc[2]);
printf("parri+0 = %p, parri+1 = %p, parri+2 = %p\n", parri+0, parri+1, parri+2);
//char* arrc[] 元素操作
printf("arrc[0] = %s, arrc[1] = %s, arrc[2] = %s\n", arrc[0], arrc[1], arrc[2]);
printf("*(parrc+0) = %s, *(parrc+1) = %s, *(parrc+2) = %s\n", *(parrc+0), *(parrc+1), *(parrc+2));
return 0;
}
*/
//指针必须初始化才可使用,及指针层面的交换问题
1:int main ( )
2:{
3: char *str[5]={"welcome","to","fortemedia","Nanjing"};
4: char * * p=str+1; //p存储 "to"字符串地址的地址,即&str[1]
5:
6: str[0]=(*p++)+2;//str[0]指向&'\0',然后p后移一位,
//存储"fortemedia"字符串地址的地址,即p = &str[2]
7: str[1]=*(p+1); //p+1 = &str[3];则 str[1] = str[3],
//即str[1]和str[3]都指向同一地址&”Nanjing”
8: str[2]=p[1]+3;//p[1]存储"Nanjing"字符串的地址,为(char *)型,
//p[1]+3相当于p[1]+sizeof(char)*3,
9: //即str[2]指向"Nanjing"字符串中的"jing"地址
10: str[3]=p[0]+(str[2]-str[1]);
//str[3]指向从p[0](也就是*p(str[2]))开始的
//偏移量为3, str[2]指向"jing",即p[0]指向"jing"
11: //所以str[3]指向"jing"的"g"地址
12: printf("%s\n",str[0]); //输出'\0',也即换行
13: printf("%s\n",str[1]); //输出"Nanjing"
14: printf("%s\n",str[2]); //输出"jing"
15: printf("%s\n",str[3]); //输出"g"
16:
17: return 0;
18:}
图解注释:
代码行4:
代码行6:
代码行7:
代码行8:
代码行10:
Other one
1:int main()
2:{
3: char *str[5] = {"welcome","to","fortemedia","Nanjing"};
4: char **p = str + 1; //p存储 "to"字符串地址的地址,即&str[1]是”to”
5: str[0] = *p++; //首先p++,则此时p此时存储了&str[2]时”fortemedir”
//p++仍是p加一之前的值
6: //str[0]相当于str[1]相当于”to”
7: str[1] = *(p+1); //*(p+1)的结果相当于&"Nanjing"
//故str[1]相当于&"Nanjing",相当于str[3]
8: str[2] = p[1] + 3; //p[1]相当于*(p+1),相当于&"Nanjing"
//然后*(p+1)+sizeof(char)*3,则此时str[2]存储
9: //str[2]相当于"Nanjing"字符串中的子字符串&"jing"
10: str[3] = p[0] + (str[2] - str[1]); //str[2] - str[1]的结果为3,
//第五行p[0]相当于str[2],目前存储了(第九行)&"jing"
11: //故str[3]相当于&"g"
14: printf("%s\n",str[0]); //输出"to"
15: printf("%s\n",str[1]); //输出"Nanjing"
16: printf("%s\n",str[2]); //输出"jing"
17: printf("%s\n",str[3]); //输出"g"
18: return 0;
19:}
以下是对上述代码关键行的图解注释:
代码行4:
代码行5:
代码行7:
代码行8:
代码行10: