C++ primer plus c++基础学习中的一些难点

#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)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值