c - Pointers for a beginner (with code) -
i doing first ever homework assignment in c , i'm trying grasp pointers. make sense in theory, in execution i'm little fuzzy. have code, supposed take integer x, find least significant byte, , replace y byte in same location. gcc returns with:
"2.59.c:34:2: warning: passing argument 1 of ‘replace_with_lowest_byte_in_x’ makes pointer integer without cast [enabled default]
2.59.c:15:6: note: expected ‘byte_pointer’ argument of type ‘int’"
and same argument 2. kind explain me going on here?
#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"); } void replace_with_lowest_byte_in_x(byte_pointer x, byte_pointer y) { int length = sizeof(int); show_bytes(x, length); show_bytes(y, length); int i; int lowest; lowest = x[0]; (i=0; < length; i++) { if (x[i] < x[lowest]) { lowest = i; } } y[lowest] = x[lowest]; show_bytes(y, length); } int main(void) { replace_with_lowest_byte_in_x(12345,54321); return 0; }
the function expects 2 pointers you're passing integer(-constant)s. want put numbers in own variables , pass addresses of function: (in main
):
int = 12345, b = 54321; replace_with_lowest_byte_in_x(&a, &b);
note you're still passing incompatible pointers.
Comments
Post a Comment