c - 使用指针时的错误和警告消息

标签 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 ===|

我如何才能修复这些错误和警告? 预先感谢您。

最佳答案

错误消息说明了一切:

calcoutput 采用 int* 作为其第五个参数,但您传递给它的是 double*。将第五个参数更改为 double*。

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

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

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

实际上你根本不需要强制转换,结果可以隐式转换为 double 。

关于c - 使用指针时的错误和警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13020219/

相关文章:

c - 在 char 设备中,我应该把 ioread 放在哪里?

c - 多维数组的typedef?

actionscript-3 - AS3 : How to force parameters in callback functions

c# - 基于输入的动态返回类型 - asp.net

c - 将指针传递给多维数组

c++ - 通过套接字发送 BYTE*

c - 段错误 - 双向链表

c - fgets 在没有接受输入的情况下失败

php - 引用类变量作为 PHP 函数中的默认参数

c++ - 在C中如何使用指针/数组来实现这些方法?