1.
Code:
#include <stdio.h>typedef unsignedChar*Byte_pointer;voidShow_bytes (byte_pointer start,intLen) { inti; for(i =0; i < Len; i++) {printf ("%.2x", Start[i]); } printf ("\ n");}voidShow_int (intx) {show_bytes (Byte_pointer)&x,sizeof(int));}voidShow_float (floatx) {show_bytes (Byte_pointer)&x,sizeof(float));}voidShow_pointer (void*x) {show_bytes (Byte_pointer)&x,sizeof(void*));}voidTest_show_bytes (intval) { intIval =Val; floatFVal = (float) ival; int*pval = &ival; printf ("%x\n", PVal); Show_int (ival); Show_float (FVal); Show_pointer (pval);}intMain () {test_show_bytes (12345);}
Operation Result:
E4 3c (7f 00 00).
The invocation of the function Show_int (), Show_float () is better understood.
Show_int () &x takes the actual address. To get the address to point to the contents of each byte in the data , a type conversion is required, that is, to convert the int * to unsigned char *, but after the type conversion has not known the length of the data, it is necessary to manually pass in the parameter of the data length, namely sizeof (int). Thereafter, the content that each pointer points to (unsigned char form) is output in 16 binary form. Show_float () is similar.
int *pval = &val Run, PVal is a pointer, 64 bits, whose contents point to the address of Val. printf ("%x\n", pval) is the output address in 16 binary form, but because%x can only represent 32 bits, so remove the high 32 bits, become 3c 18, the small end mode output is 3C 68 56
"Csapp" Reading notes-Chapter 2. Representing and manipulating information