int a[5]={}, &a+1与(int*)a+1的区别

#include <iostream> 
#include <typeinfo> 
using namespace std;
  
int  main()  
{ 
    int b, *pb;  
    char *pb2;  
    char *pb3;  
    //&b = 0x001af74
    pb = &b + 1;  //0x001af78   int*
    pb2 = (char*)&b + sizeof(b);  //0x001af78  char*
    pb3 = (char*)&b + 1; //0x001af75   char*
    printf("&b=%#x, pb=%#x, pb2=%#x,pb3=%#x\n", &b, pb, pb2, pb3);  
  
    int a[5]={1,2,3,4,5}, *pa;  
    char *pa2;  
    //&a = 0x002cfc00    int[5]*
    pa = (int*)(&a + 1);  //pa = 0x002cfc14  int*
    pa2 = (char*)&a + sizeof(a);  //pa2 = 0x002cfc14   char*
    //(int*)&a + 1 = 0x002cfc04          &a + 1 = 0x002cfc14
    printf("\nDiff: &a=%#x, %#x, %#x\n", &a, (int*)&a + 1, &a + 1); // 两者是有区别的  
  
    printf("&a=%#x, pa=%#x, pa2=%#x\n", &a, pa, pa2);  
    printf("Equal2=%d\n", (void*)pa == (void*)pa2 );  
  
    puts("结论: &a + 1 == (char*)&a + sizeof(a)\n");  
  
    return 0;
}

 

posted @ 2015-09-09 19:44  无天666  阅读(959)  评论(0编辑  收藏  举报