0

I have this C program:

#include <stdio.h>
#include <math.h>
main() {
  int I;
  double A[3]={0.0, 1.0, 2.0};
  double B[3]={0.0, 1.0, 2.0};
  double C[3]={0.0, 1.0, 2.0};
  double X[3];

  for (I=0; I<3; I++) {
      X[I] = A[I] * ( B[I] - C[I] )**2;
  }
}

compiling produces an error:

invalid type argument of 'unary *' (have 'int')

How should I fix this?

Micha Wiedenmann
  • 17,330
  • 20
  • 79
  • 123
Claire Sun
  • 23
  • 2
  • 5
    What do you think `a**2` does? – hellow Oct 08 '18 at 11:59
  • Also, take a look at https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function or even better, read a _good_ C book. I think you need one :) – hellow Oct 08 '18 at 12:00
  • There is no ** operator in C. – Yu Hao Oct 08 '18 at 12:00
  • In some languages the `**` is to indicate an exponent. C is not one of these. – Jongware Oct 08 '18 at 12:00
  • By the way, it would really really really help (more) if you provide as much additional information as you can. I bet the compiler also gave a specific line number. In this case, anyone with a `c` badge spotted the error from a mile away, but don't count on this. – Jongware Oct 08 '18 at 12:03

1 Answers1

0

Here you are presumably trying to square an expression.

X[I] = A[I] * ( B[I] - C[I] )**2;

Do it like this instead:

X[I] = A[I] * pow(B[I] - C[I], 2.0);

I don't think there's an integer pow in math.h, but this answer provides a nice implementation.

Blaze
  • 16,456
  • 1
  • 21
  • 43
  • Integer `pow` is of no relevance to the `double B[3]`and `double C[3]`. – Eric Postpischil Oct 08 '18 at 13:20
  • That would be a heterogeneous integer-floating-point `pow`, not an integer `pow`, and the link you give is for a pure integer `pow`. A `pow(double, int)` could be useful for some limited cases, but a good compiler will optimize `pow(x, 2)` to `x*x` anyway. – Eric Postpischil Oct 08 '18 at 13:25
  • Right, sorry. That's what I had in mind - and linked the wrong one anyway. I'd assume it's a simple modificiation to the int, int one however. :) – Blaze Oct 08 '18 at 13:27