我的代码中有一些错误,我不知道如何解决,我认为我将函数调用到 main 错误,但我的程序停止在
#include <cs50.h>
#include <stdio.h>
//is the size of the array
const int n = 3;
int sum_side(float x[]);
int main(void)
{
float sum;
int a[n];
sum = sum_side(a);
// is used to get 3 sides from the user all have to be positive
do
{
a[0] = get_int("First side: ");
a[1] = get_int("Second side: ");
a[2] = get_int("Third side: ");
}
while(a[0] <= 0 || a[1] <= 0 || a[2] <=0);
//is used to say if it can be a triangle or nor
if(sum_side(a[n]) == 1)
{
printf("Can be a triangle? ");
}
else
{
printf("Can't be a triangle? ");
}
}
int sum_side(float x[])
{
// Check that sum of any two sides greater than third
if ((x[0] + x[1] <= x[2]) || (x[1] + x[2] <= x[0]) || (x[2] + x[0] <= x[1]))
{
return false;
}
// if we passe the test, we're good!
return true;
}
如果有人可以给我一些如何解决的建议或指示
#1 启用所有编译器警告。
如果OP从这篇文章中学到了什么,那就是使用本地工具,就像许多编译器警告一样,来提高生产力并减少调试时间。
带有 OP 代码的警告示例:
warning: passing argument 1 of 'sum_side' from incompatible pointer type [-Wincompatible-pointer-types]
14 | sum = sum_side(a);
| ^
| |
| int *
note: expected 'float *' but argument is of type 'int *'
7 | int sum_side(float x[]);
| ~~~~~~^~~
warning: conversion from 'int' to 'float' may change value [-Wconversion]
14 | sum = sum_side(a);
| ^~~~~~~~
warning: passing argument 1 of 'sum_side' makes pointer from integer without a cast [-Wint-conversion]
26 | if(sum_side(a[n]) == 1)
| ~^~~
| |
| int
note: expected 'float *' but argument is of type 'int'
7 | int sum_side(float x[]);
| ~~~~~~^~~
warning: variable 'sum' set but not used [-Wunused-but-set-variable]
11 | float sum;
| ^~~