使用指针时出现错误和警告消息

时间:2012-10-22 21:31:43

标签: c function pointers

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CONST 267


void getInput(int *length, int *width, int *height);
void calcoutput(int length, int width, int height, int *squareFootage,int         *paintNeeded);
int getSquareFootage(int length,int width, int height);
double getPaintNeeded(int squareFootage);


int main(void)
{
    int length;
    int width;
    int height;
    int squareFootage;
    double paintNeeded;


    getInput(&length, &width, &height);
    calcoutput(length, width, height,&squareFootage,&paintNeeded);


    return 0;
}   //end main

void getInput(int *plength, int *pwidth, int *pheight)
{
    printf("Enter the length of the room (whole number only): ");
    scanf("%d", plength);
    printf("Enter the width of the room (whole number only): ");
    scanf("%d", pwidth);
    printf("Enter the height of the room (whole number only): ");
    scanf("%d", pheight);
}   //end getInput
void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded){

    *squareFootage = getSquareFootage(length,width, height);
    *paintNeeded = getPaintNeeded(squareFootage);

}

int getSquareFootage(int length,int width, int height){
    int i;
    i = 2*(length* height) + 2*(width*height) + (length* width);
return i;
}
double getPaintNeeded(int squareFootage)
{
    double i = double (squareFootage / CONST);
    return i;
}

我正在编写这段代码来计算房间的面积和绘制房间所需的油漆加仑数,但是,我对C中的指针不是很熟悉,似乎有一些错误和警告像这个

C:\Users\khoavo\Desktop\hw2b.c||In function 'main':|
C:\Users\khoavo\Desktop\hw2b.c|23|warning: passing argument 5 of 'calcoutput' from incompatible pointer type|
C:\Users\khoavo\Desktop\hw2b.c|8|note: expected 'int *' but argument is of type 'double *'|
C:\Users\khoavo\Desktop\hw2b.c||In function 'calcoutput':|
C:\Users\khoavo\Desktop\hw2b.c|41|warning: passing argument 1 of 'getPaintNeeded' makes integer from pointer without a cast|
C:\Users\khoavo\Desktop\hw2b.c|10|note: expected 'int' but argument is of type 'int *'|
C:\Users\khoavo\Desktop\hw2b.c||In function 'getPaintNeeded':|
C:\Users\khoavo\Desktop\hw2b.c|52|error: expected expression before 'double'|
||=== Build finished: 1 errors, 2 warnings ===|

我如何能够修复这些错误并发出警告? 提前谢谢你。

3 个答案:

答案 0 :(得分:1)

错误消息说明了一切:

calcoutput将int *作为其第五个参数,但是你传递了一个double *。将第五个参数更改为double *。

getPaintNeeded接受一个int,但你传递一个int *。我认为在这种情况下你想要的是getPaintNeeded(*squareFootage)

最后一个错误是关于演员。您正在使用C ++支持但不在C中支持的函数式转换,并且您正在编译为C.要么编译为C ++(将文件扩展名更改为.cpp),要么将行更改为:

double i = (double)(squareFootage / CONST);

实际上你根本不需要演员,结果可以隐含地转换为双击。

答案 1 :(得分:0)

更改

void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded);

void calcoutput(int length, int width, int height, int *squareFootage,double *paintNeeded);

答案 2 :(得分:0)

paintNeeded被声明为double,但是你将它的位置作为指向int的指针传递。您传递给它的函数将以整数而不是double的形式看到该值,这将使您的程序运行不正确。

您应该考虑将calcoutput中的int *转换为double *,以便传入paintNeeded行为正确。