//csapp 2.60
#include <stdio.h>
#include <inttypes.h>
typedef unsigned char * char_point;
uint32_t replace_byte(uint32_t x, int i, unsigned char b)
{
if(i > 3 || i < 0) {
return -1;
}
char_point char_date_point = ((char_point) &x) + i;
*char_date_point = b;
printf("result : %X\r\n", x);
return x;
}
int main(void)
{
unsigned char replace_data = 0xAB;
uint32_t x = 0x12345678;
uint32_t r_x = 0x12AB5678;
int i_x = 2;
uint32_t y = x;
uint32_t r_y = 0x123456AB;
int i_y = 0;
printf("r_x == result : %d\r\n", r_x == replace_byte(x, i_x, replace_data));
printf("r_y == result : %d\r\n", r_y == replace_byte(y, i_y, replace_data));
return 1;
}
原文:https://www.cnblogs.com/LonelyTraveler/p/12853105.html