c - Pointer Error: Makes pointer from integer without a cast -
i'm brand new c, , i'm trying write function replaces specific byte in unsigned int. pointers still have me little fuzzy--would care explain me error conceptually these pointers in replace_byte()? in advance :)
#include <stdio.h> typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int length) { int i; (i=0; < length; i++) { printf(" %.2x", start[i]); } printf("\n"); } unsigned replace_byte(unsigned x, int i, unsigned char b) { int length = sizeof(unsigned); printf("x: "); show_bytes(x, length); printf("replace byte position left: %u\n", i); printf("replace with: %u\n", b); printf("combined: "); int locationfromright = (length - - 1); x[locationfromright] = b; show_bytes( (byte_pointer)&x, length); } int main(void) { unsigned = 0x12345678; int loc = 2; unsigned char replacewith = 0xab; replace_byte(a, loc, replacewith); return 0; }
your function definition
void show_bytes(byte_pointer start, int length) takes pointer first argument
typedef unsigned char *byte_pointer; however in function replace_byte()
unsigned replace_byte(unsigned x, int i, unsigned char b) you pass x declared type unsigned show_bytes()
show_bytes(x, length);
Comments
Post a Comment