gpt4 book ai didi

C 中的编译器错误 C2440

转载 作者:行者123 更新时间:2023-11-30 20:42:52 25 4
gpt4 key购买 nike

我正在完成我在 C 书中找到的一个程序,但遇到了一些问题。我即将完成,但我不断收到此错误错误 1 ​​错误 C2440:'function':无法从 'double [15]' 转换为 '为什么我会收到此编译器错误?

void arrayRead(double, int*);

void arrayList(double, int*);

void arraySum(double, int*, int*);

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<ctype.h>
int main()
{
int num, sum = 0;
double array[15];
printf("How many doubles (numbers) would you like in the array (20 max)?\n");
scanf("%d", &num);
printf("Thank you! Now give me %d different doubles (numbers) please!\n", num);

arrayRead(array, &num);

printf("Here are all of your integers again!\n");

arrayList(array, &num);

arraySum(array, &num, &sum);
printf("The sum of these numbers = %d\n", sum);
return 0;
}

void arrayRead(double array[], int* num)
{
for (int i = 0; i < *num; i++)
{
scanf("%lf", &array);
}
}

void arrayList(double array[], int*num)
{
for (int i = 0; i < *num; i++)
{
printf("%.2f\n", array);
}
}

void arraySum(double array[], int*num, int* sum)
{
for (int i = 0; i < *num; i++)
{
*sum = array + *sum;
}
}

最佳答案

最好将原型(prototype)放置在任何 #include 语句之后,以防万一原型(prototype)使用头文件中定义的内容

数组的大小必须是正数,所以使用 '%u' 输入正数并将 max 变量定义为无符号

函数的参数以及调用这些函数时传递的参数需要具有匹配的类型(或者函数类型是传递类型的“提升”。

由于数组是double,因此所有引用也应该是double,就像sum变量一样。

适当的水平空白使代码更易于阅读

不要#include那些内容未被使用的头文件。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//#include <ctype.h>

// prototypes
void arrayRead( double*, unsigned );
void arrayList( double*, unsigned );
void arraySum ( double*, unsigned, double* );

int main( void )
{
unsigned num;
double sum = 0.0;

printf( "How many doubles (numbers) would you like in the array (20 max)?\n" );
if( 1 != scanf( "%u", &num ) )
{
perror( "scanf for size of array failed" );
exit( EXIT_FAILURE );
}

// implied else, scanf successful

// need to add check that user entered value is in range 1...20

double array[ num ];

printf( "Thank you! Now give me %u different doubles (numbers) please!\n", num );
arrayRead( array, num );

printf( "Here are all of your integers again!\n" );
arrayList( array, num );

arraySum( array, num, &sum );
printf("The sum of these numbers = %lf\n", sum);
return 0;
} // end function: main


void arrayRead( double array[], unsigned num )
{
for ( unsigned i = 0; i < num; i++ )
{
scanf("%lf", &array[i]);
}
} // end function: arrayRead


void arrayList( double array[], unsigned num )
{
for ( unsigned i = 0; i < num; i++ )
{
printf("%.2f\n", array[i]);
}
} // end function: arrayList


void arraySum( double array[], unsigned num, double* sum )
{
for ( unsigned i = 0; i < num; i++ )
{
*sum = array[i] + *sum;
}
} // end function: arraySum

以下是简单运行答案代码的输出:

How many doubles (numbers) would you like in the array (20 max)?
5
Thank you! Now give me 5 different doubles (numbers) please!
1 1 1 1 1
Here are all of your integers again!
1.00
1.00
1.00
1.00
1.00
The sum of these numbers = 5.000000

关于C 中的编译器错误 C2440,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com