#include <iostream>
using namespace std;
int main(){
char * pa[10] = {"name", "hello", "world", "you","peace", "love"};
int arr[10] = {1,2,3,4,5,6,7};
int (*pp)[10] = &arr;
cout << sizeof(pp) << endl;
cout << sizeof(pa) << endl;
cout <<sizeof(arr) << endl;
}
root@eiaelch-VirtualBox:/repo/c_demo# ./arry_pointer
8
80
40
char * pa[10]声明了一个存储10个char*指针的数组,因此长度为80;
int arr[10] ,其中arr只是一个int数组,所以arr的长度为40
而int (*pp)[10] = &arr;pp则是一个指向整个arr数组的指针,所以是一个数组的长度,和arr本身并没有太大关系。
#include <iostream>
using namespace std;
int main(){
char * pa[10] = {"name", "hello", "world", "you","peace", "love"};
int arr[10] = {1,2,3,4,5,6,7};
int * pt = arr;
int (*pp)[10] = &arr;
int value = *++pt;
cout << sizeof(pp) << endl;
cout << sizeof(pa) << endl;
cout << *pt++ << endl;
}
8
80
2
* ++pt, 前缀++和*的优先级相同,先执行++操作,再解除引用: pt[1];
++*pt,如上,先将pt地址指向的值取出来,然后++;
*pt++ 后缀优先级比*高,先执行++操作,然后解除引用,先使用pt[1], 该语句执行完成之后,指向pt[2];
#include <iostream>
using namespace std;
enum {red = 1, blue =0, yellow = 2};
int main(){
char* pt = "hello";
char arr[10] = "hello";
cout << (int*) arr << endl << (int*)"hello" << endl << (int*)pt << endl;
*(pt + 1) = 'a'; //operation permitted, but after operation, the memory is not legal again
//so print will show a unlegal message
cout << pt << endl;
return 0;
}
root@eiaelch-VirtualBox:/repo/c_demo# ./demo
0x7ffcddc96a5e
0x559405f31b55
0x559405f31b55
Segmentation fault (core dumped)