gpt4 book ai didi

c - 公式抛出错误 'called object is not a function'

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:28 27 4
gpt4 key购买 nike

我正在用 C 语言做一个练习,让我将数据输入到一个结构中,然后在一个单独的函数中对其进行操作。但是当程序运行到进行实际数学计算的那一行时,我得到一个错误,关于一个被调用的对象不是一个函数。

这是确切的错误:

p1s2.c:70:106: 错误:调用对象'(vectorArray + (sizetype)((unsigned int)i * 32u))->y * (vectorArray + (sizetype)((unsigned int) i * 32u))->y' 不是函数

我会提前为代码道歉,这是一个正在进行的文件,所以它不是很干净。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Structure declaration.
struct vector {
double x; //X-coordinate for vector
double y; //Y-coordinate for vector
double z; //Z-Coordinate for vector
double length; //Length. Calculations will go here.
};

int howLong(struct vector *x);
int main(void)
{
int arraySize;
int i;
int vectorNum=1;
int retval; //Watch for counter in howLong.
int scanval; //Error checking for coordinates input (scanf statement)

printf("How many vectors would you like to calculate length for?\n");
scanf("%d", &arraySize);
//Allocate memory to struct.
struct vector *vectorArray = malloc(arraySize*sizeof(double));

printf("You will now enter the coordinates for %d vectors. \n", arraySize);
//Input loop.
for(i=0; i<=arraySize; i++){
printf("Please enter the X, Y, Z coordinates for vector %d. \n", vectorNum);
printf("Please separate the coordinates with spaces. \n");

int scanval; //Error checking: Scanval should be equal to three.
//scanf takes user input, converts to long float.
scanval=(scanf("%lf %lf %lf", &vectorArray[i].x, &vectorArray[i].y, &vectorArray[i].z));
if(scanval !=3) {
printf("You can't follow directions. That's too bad. \n");
exit(0);
}
//Print input back to user.
printf("Vector Number %d: %lf %lf %lf \n", vectorNum, vectorArray[i].x, vectorArray[i].y, vectorArray[i].z);
//Increment counters.
vectorNum++;
i++;
}
for(i=0; i<=arraySize; i++){
vectorArray[i].length=howLong(vectorArray);
i++;

}
}

这是外部函数 howLong:

 int howLong(struct vector *vectorArray) 
{ //Function gets the struct and coordinate values, calculates length and writes to struct.
int i;
int vectorNum=1;
printf("Calculating vector length. \n");
//Math.
vectorArray[i].length = sqrt((vectorArray[i].x * vectorArray[i].x)+(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z));
//Error occurs on the line directly above this comment. ^^

printf("Length of Vector Number %d: %lf \n", vectorNum, vectorArray[i].length);
return vectorArray[i].length;
}

我不明白。一开始以为是函数名的问题,改成howLong后还是报错。有任何想法吗?

最佳答案

好吧,这里:

(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z)

在两个 () 部分之间缺少一个加号。

关于c - 公式抛出错误 'called object is not a function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104111/

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