Visual Studio中對指針內存的學習筆記(一)

#include "stdio.h"
int main(){
   int a[5]={1,2,3,4,5};
   int (*b)[5]=&a;

<pre name="code" class="cpp">//數組指針。本質是指針。sizeof(b)是求int型指針的大小爲4.*b代表是具有5個int型變量的數組,所以sizeof(*b)=20
   printf("%d\n",sizeof(*b)); //20
printf("%d\n",sizeof(a)); //20
printf("%d\n",sizeof(a+1)); //4
printf("%d\n",sizeof((int *)a)); //4

// &a+1會跳過整個數組(4*5個字節) // &a爲int(*)[5]類型 a爲int *類型,雖然這兩個的值是一樣的,但是含義不一樣// &a[0]+1會跳過數組的一個int元素(4個字節)// (int)a+1會跳過一個字節,數組的一個int元素爲4個字節 int *ptr1=(int *)(&a+1); //ptr指向數組後的一個未知變量值,則ptr1[-1]指針回跳4個字節(因爲ptr爲int型指針)int *ptr2=(int *)((int)a+1);printf("%d\n",&a+1);//1964320printf("%d\n",&a[0]+1);//1964304printf("%d\n",(int)&a[0]+1);//1964301printf("%x,%x",ptr1[-1],*ptr2);//則ptr1[-1]爲a這個數組的最後一個元素 5 ;*ptr2 輸出爲2000000(2 00 00 00) getchar(); return 0;}

 


這裏需要考慮存儲模式:大端模式和小端模式。
大端模式(Big_endian):字數據的高字節存儲在低地址中,而字數據的低字節則存放在高地址中。
小端模式(Little_endian):字數據的高字節存儲在高地址中,而字數據的低字節則存放在低地址中。

參考網頁:C語言union關鍵字   http://see.xidian.edu.cn/cpp/html/450.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章