使用指针时的错误和警告消息 [英] errors and warning message when using pointer

查看:103
本文介绍了使用指针时的错误和警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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中的指针不是很熟悉,似乎有一些错误和警告,例如

i'm writing this code to calculate the area of the room and number of gallons of paint needed to paint the room, however, I'm not very familiar with pointers in C, there seems to be some errors and warning like this

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

我将如何解决这些错误和警告? 预先谢谢你.

how will I be able to fix these errors and warning ? thank you in advance.

推荐答案

错误消息说明了一切:

calcoutput将int *作为其第五个参数,但是您将其传递给double *.更改第五个参数为double *.

calcoutput takes an int* as its fifth argument but you're passing it a double*. Change the fifth parameter to take a double*.

getPaintNeeded需要一个int,但您正在将它传递给一个int *.我想在这种情况下,您想要的是getPaintNeeded(*squareFootage).

getPaintNeeded takes an int but you're passing it an int*. I think what you want in this case is getPaintNeeded(*squareFootage).

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

The last error is about the cast. You're using a function-style cast which supported in C++ but not in C, and you're compiling as C. Either compile as C++ (change the file extension to .cpp), or change the line to:

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

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

Actually you don't need a cast at all, the result can implicitly be converted to a double.

这篇关于使用指针时的错误和警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆