没有指针的指针错误

时间:2014-02-25 10:37:32

标签: c

我正在制作一个C程序,它可以读取圆形,正方形或三角形,并根据用户输入计算区域。我是用结构做的。我收到一些运行时错误和一些编译器错误。

每当我执行时,都会出现三角形选项,并且区域不正确。我试图刷新输入缓冲区,但它仍然会发生,为什么会这样?

即使编译编译器告诉我: “警告:赋值在没有强制转换的情况下从指针生成整数[默认启用]。

这是什么意思,你如何解决它?此程序中的指针也在哪里,因为它对我来说不是很明显/

 #include <stdio.h>

 struct point { int x, y; };

 struct shape {
 char shape_kind; 
 struct point centre;
 float area;
 union{
    struct{
     int height;
     int width;
 } rectangle;

 struct{
     int height;
     int width;
 } orthogonal_triangle;

 struct{
     int radius;
 } circle;
  }u;
};
int areaCalc(struct shape s, char shape_kind);

struct shape s;

 int main(shape_kind){
fseek(stdin,0,SEEK_END);


printf("\nWould you like to calulcate the area of a Triangle, Rectangle, or Circle?\n");
scanf("%s", &shape_kind);

fseek(stdin,0,SEEK_END);


if((shape_kind = "Triangle") || (shape_kind = "triangle")){
    printf("\nYou have selected Triangle. Please enter the base, followed by the height.\n");
    scanf("%d",&s.u.orthogonal_triangle);
    scanf("%d",&s.u.orthogonal_triangle);
}

else if((shape_kind = "Rectangle") || (shape_kind = "rectangle")){
    printf("\nYou have selected Rectangle. Please enter the width followed by the height.\n");
    scanf("%d",&s.u.rectangle.width);
    scanf("%d",&s.u.rectangle.height);
}

else if((shape_kind = "Circle") || (shape_kind = "circle")){
    printf("\nYou have entered Circle. Please enter the radius. \n");
    scanf("%d", &s.u.circle.radius);
}

else{
    printf("\nI am sorry, but your input could not be read. Please try  again.\n");
    main();
}

areaCalc(s,shape_kind);
return 0;
}

int areaCalc(struct shape s, char shape_kind){
if((shape_kind = "Rectangle") || (shape_kind = "rectangle")){
    s.area = s.u.rectangle.height*s.u.rectangle.width;
}

else if((shape_kind = "Triangle") || (shape_kind = "triangle")){
    s.area = s.u.orthogonal_triangle.height *1/2*s.u.orthogonal_triangle.width;
}

else if((shape_kind = "Circle") || (shape_kind = "circle")){
    s.area = s.u.circle.radius*s.u.circle.radius*3.1415926235;
}

else{
    printf("\nI'm sorry your input could not be read, please try again.\n");
    main();
}

printf("The total area of the %s is %f", shape_kind, s.area);

return 0;
}

4 个答案:

答案 0 :(得分:3)

有几个错误:

  1. 如果你想要scanf()像“Triangle”或“Circle”这样的字符串,你需要一个数组:

     char shape_kind[80];
     ...
     scanf( "%s" shape_kind );
     /* or safer: */
     scanf( "%79s", shape_kind );
    
  2. 您无法将字符串与=进行比较。这是一个任务(我猜这些是导致编译器警告的行)。您可以使用==来比较整数,但对于字符串,您需要strcmp()

    if( strcmp( shape_kind, "Triangle" ) == 0 )
        ....
    
  3. 顺便说一下: 下次请正确缩进你的代码,如果你有编译器错误或警告,请告诉我们它们出现的位置

答案 1 :(得分:1)

a)=是作业,==是比较 b)字符串(字符串数组)或一般数组不能在元素方面与==进行比较 使用strncmp等 c)如果指针错误仍然存​​在,请告诉我们

答案 2 :(得分:0)

shape_kind = "Triangle"如何在c中对字符串进行comapare,它不是有效的。==或使用strcmp

同样scanf("%s", &shape_kind);无效删除&我知道它是主要警告,但警告也会在增长时导致错误。

int areaCalc(struct shape s, char shape_kind)无效,无法接收shape_kind char* shape_kind并在您的主要或全球首先宣布char shape_kind[100]

另外你说Even though it compiles the compiler tells me: "warning: assignment makes integer from pointer without a cast[enabled by default].它说的是因为你没有在每次shape_kind比较时比较字符串,但你只是将字符串的指针分配给该变量。

答案 3 :(得分:0)

要比较字符串,您需要strcmpstrncmp

例如strcmp

int strcmp ( const char * str1, const char * str2 );
  

返回一个表示其间关系的整数值   strings:零值表示两个字符串相等。一个值   大于零表示第一个字符没有   match在str1中的值大于在str2中的值;并且值小于   零表示相反。

看看here

if (strcmp (str1,str2) == 0) { /* strings are equal */ }