简体   繁体   English

C基本语言-被调用的对象不是函数或函数指针

[英]C basic - called object is not a function or function pointer

That's my code - 那是我的代码-

main() 
{

    double x;
    double y = pow(((1/3 + sin(x/2))(pow(x, 3) + 3)), 1/3);
    printf("%f", y);

    return 0;
}

I get an error in double y = pow((1/3 + sin(x/2))(pow(x, 3) + 3), 1/3); 我在double y = pow((1/3 + sin(x/2))(pow(x, 3) + 3), 1/3); , it says that called object is not a function or function pointer. ,它表示被调用的对象不是函数或函数指针。 I don't get it though - (1/3 + sin(x/2))(pow(x, 3) + 3) is the first element of pow(x, y); 我不明白- (1/3 + sin(x/2))(pow(x, 3) + 3)pow(x, y);的第一个元素pow(x, y); that is the x I want to raise to y (1/3) power. 那就是我想提高到y(1/3)幂的x。 Where lies the problem? 问题出在哪里? I'm pretty new to c basic but I can't find the answer anywhere. 我对C Basic还是很陌生,但在任何地方都找不到答案。

That's because the return value of pow is a double, not a function. 这是因为pow的返回值是双pow值,而不是函数。 Maybe what you're trying to do is pass a call to pow as the second argument to the first pow. 也许您想要做的是将对pow的调用作为第一个pow的第二个参数传递。

If you want to multiply, you need to use the * operator. 如果要相乘,则需要使用*运算符。 You can't put parenthesized expressions adjacent to each other to denote multiplication. 您不能将带括号的表达式彼此相邻以表示乘法。

(1/3 + sin(x/2))*(pow(x, 3) + 3)
#include <stdio.h>
#include <math.h> // For pow() function

int main() {

    // Initialize with whatever value you want
    double x = 100;

    // Make sure to use an arithmetic operator
    double y = pow(((1/3.0 + sin(x/2))*(pow(x, 3) + 3)), 1/3.0);

    // Use right format specifier
    printf("%lf", y);

    return 0;
}
  1. Don't forget to include math.h library. 不要忘记包括math.h库。
  2. Initialize x with a value. 用值初始化x
  3. Avoid using integer division 1/3 , instead use 1/3.0 or 1.0/3.0 because 1/3 == 0 . 避免使用整数除法1/3 ,而使用1/3.01.0/3.0因为1/3 == 0
  4. Use an arithmetic operator ( + , - , * , / ) before middle pow() like pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0) . 在像pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0)中间pow() )之前使用算术运算符( +-*/pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0)
  5. (Opt.) Use right format specifier for y which is %lf . (可选)对y使用正确的格式说明符%lf But you may get away with %f . 但是您可能会与%f脱身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM