从事交流项目,我遇到了障碍。我正在尝试打印点坐标样式,并且我已经包含了所有必要的代码来帮助你们帮助我!
//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 from standard input
readPoint(){
struct Point point;
printf("Enter label (character): ");
fgets(buff, MAX_LINE, stdin);
point.label = buff; //I think this is the 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);
编译后,我收到以下错误:
points.c: In function 'displayPoint':
points.c: warning: initialization makes pointer from integer without a cast [enabled by default]
points.c: warning: format '%c' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat]
points.c: In function 'readPoint':
points.c: warning: assignment makes integer from pointer without a cast [enabled by default]
如果我使用以下信息创建一个点:
Label: A
x: 2
y: 3
并运行 displayPoint 我得到这个输出:
R: (2, 3)
显然这是错误的,我不知道 R 是从哪里来的。我在这里想念什么?为什么C一定要这么固执,这在java、python等中就这么简单