我正在使用C进行练习,我将数据输入到结构中,然后在单独的函数中对其进行操作。但是当程序进入实际数学运算的行时,我得到一个关于被调用对象不是函数的错误。
这是确切的错误:
p1s2.c:70:106: error: called object '(vectorArray + (sizetype)((unsigned int)i * 32u))->y * (vectorArray + (sizetype)((unsigned int)i * 32u))->y' is not a function
我会提前为代码道歉,这是一个正在进行中的文件,所以它不是很干净。
#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后错误仍然存在。有任何想法吗?
答案 0 :(得分:2)
嗯,这里:
(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z)
在两个()部分之间缺少加号。
答案 1 :(得分:1)
在函数howLong
((vectorArray[i].x * vectorArray[i].x)+(vectorArray[i].y * vectorArray[i].y)(vectorArray[i].z * vectorArray[i].z));
^operator is missing