所以这是问题所在:用场的半径定义圆结构,用场的长度定义方形结构。创建一个接受void *类型指示符和整数参数作为参数的函数。如果其值为0,则指针指示圆型结构,否则指示方形型结构。该函数将指针显示的结构的字段的值增加5,然后返回指针。编写一个控制该功能操作的程序。
这是我的代码:
#include <stdio.h>
struct circle{
float radius;
};
struct square{
float side;
float len;
};
void* resize(void *shape, int type);
int main(void){
circle *p1, c = {2.45};
square *p2, s ={4};
p1 = (circle*)resize(&c, 0); //Error in circle
p2 = (square*)resize(&s, 1); //Error in square
printf("%f %d\n", p1->radius, p2 ->len);
return 0;
}
void* resize(void *shape, int type)
{
if(type ==0)
((circle*)shape)->radius +=5;//Error in circle
else
((square*)shape)->len +=5; //Error in square
return shape;
}
enter code here
而且我有错误。有人知道如何解决吗?错误是:
E:\TDM-GCC\bin\untitled5\main.c: In function 'main':
E:\TDM-GCC\bin\untitled5\main.c:14:5: error: unknown type name 'circle'; use 'struct' keyword to refer to the type
circle *p1, c = {2.45};
^~~~~~
struct
E:\TDM-GCC\bin\untitled5\main.c:15:5: error: unknown type name 'square'; use 'struct' keyword to refer to the type
square *p2, s ={4};
^~~~~~
struct
E:\TDM-GCC\bin\untitled5\main.c:16:11: error: 'circle' undeclared (first use in this function)
p1 = (circle*)resize(&c, 0);
^~~~~~
E:\TDM-GCC\bin\untitled5\main.c:16:11: note: each undeclared identifier is reported only once for each function it appears in
E:\TDM-GCC\bin\untitled5\main.c:16:18: error: expected expression before ')' token
p1 = (circle*)resize(&c, 0);
^
E:\TDM-GCC\bin\untitled5\main.c:17:11: error: 'square' undeclared (first use in this function)
p2 = (square*)resize(&s, 1);
^~~~~~
E:\TDM-GCC\bin\untitled5\main.c:17:18: error: expected expression before ')' token
p2 = (square*)resize(&s, 1);
^
E:\TDM-GCC\bin\untitled5\main.c:18:25: error: request for member 'radius' in something not a structure or union
printf("%f %d\n", p1->radius, p2 ->len);
^~
E:\TDM-GCC\bin\untitled5\main.c:18:38: error: request for member 'len' in something not a structure or union
printf("%f %d\n", p1->radius, p2 ->len);
^~
E:\TDM-GCC\bin\untitled5\main.c: In function 'resize':
E:\TDM-GCC\bin\untitled5\main.c:25:11: error: 'circle' undeclared (first use in this function)
((circle*)shape)->radius +=5;
^~~~~~
E:\TDM-GCC\bin\untitled5\main.c:25:18: error: expected expression before ')' token
((circle*)shape)->radius +=5;
^
E:\TDM-GCC\bin\untitled5\main.c:27:11: error: 'square' undeclared (first use in this function)
((square*)shape)->len +=5;
^~~~~~
E:\TDM-GCC\bin\untitled5\main.c:27:18: error: expected expression before ')' token
((square*)shape)->len +=5;
^
mingw32-make.exe[3]: *** [CMakeFiles\untitled5.dir\build.make:62: CMakeFiles/untitled5.dir/main.c.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/untitled5.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/untitled5.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: untitled5] Error 2
您不能使用'circle',您不能使用其他结构。
您需要使用'结构圈'等。>>
如果您不想使用“结构圈”,可以这样做:
typedef struct circle{ float radius; } Circle;
并且只使用'Circle'