c - Issues with fgets and printf..printing a string -
working on c project , i've hit roadblock. i'm trying print point coordinate style, , i've included code necessary folks me out!
//point.h struct point{ char label; int x; int y; }; //point.c displaypoint(struct point p){ char label = p.label; int x = p.x; int y = p.y; // label: (x, y) printf("(%c: (%d, %d)", label, x, y); } //build point standard input readpoint(){ struct point point; printf("enter label (character): "); fgets(buff, max_line, stdin); point.label = buff; //i think issue here printf("enter x:"); fgets(buff, max_line, stdin); point.x = (int) strtol(buff, null, max_line); printf("enter y:"); fgets(buff, max_line, stdin); point.y = (int) strtol(buff, null, max_line);
upon compilation, receive following error:
points.c: in function 'displaypoint': points.c: warning: initialization makes pointer integer without cast [enabled default] points.c: warning: format '%c' expects argument of type 'int', argument 2 has type 'char *' [-wformat] points.c: in function 'readpoint': points.c: warning: assignment makes integer pointer without cast [enabled default]
if create point using following information:
label: x: 2 y: 3
and run displaypoint output:
r: (2, 3)
obviously that's wrong, don't know r came from. missing here? why c have stubborn, simple in java, python, etc
you've got couple of problems here:
fgets
gets buffer, not single character. hopebuff
declared character array (char buff[max_line]
).your struct declares single character label, you're assigning buffer character.
you're reusing buffer. because pointer chunk of memory, you're potentially clobbering old data. might (read, want to) reset buffer between uses.
given this, think intend this:
point.label = buff[0];
i'm guessing want single character label, in case should work , won't clobbering value. (i don't know rest of input.)
Comments
Post a Comment