"Csapp" Reading notes-Chapter 2. Representing and manipulating information

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.