2016-08-26 27 views
0

我想知道的功能show_int()低於實際做...C代碼是什麼意思? - 計算機系統程序員的角度

此代碼是在計算機系統程序員的角度來看的第28頁。

#include <stdio.h> 

typedef unsigned char *byte_pointer; 

void show_bytes(byte_pointer start, int len) { 
    int i; 
    for (i = 0; i < len; i++) { 
     printf("%.2x", start[i]); 
    } 
    printf("\n"); 
} 

void show_int(int x) { 
    show_bytes((byte_pointer) &x, sizeof(int)); 
} 

void main() { 
    show_int(20); 
    getchar(); 
} 

回答

0

需要理解的最重要的事情是要轉換成*byte_pointer(又名unsigned char):

(byte_pointer) &x 

你可以把它當作轉換指針int(你的情況:20)進行一系列(可以是4或8甚至更多字節,取決於體系結構)。

然後show_bytes()函數正在迭代一個字節數組以顯示其後續字節,並將其格式化爲hexadecimal format