第二題:考查類型轉換
#include <stdlib.h>
#include <stdio.h>
int main()
{
struct node
{
int a;
int b;
int c;
};
struct node s= { 3,5,6 };
struct node *pt = &s;
printf("%d\n" , *(int*)pt);
return 0;
}
這段程序的輸出是:
(a)3 (b) 5 (c) 6 (d) 7
解答:
這個題很簡單,主要考察類型強制轉化問題:
結構體的成員在內存中的地址是按照他們定義的位置順序依次增長的。如果一個結構體的指針被看成它的第一個成員的指針,那麼該指針的確指向第一個成員