4

I have had trouble with this piece of code returning the error:

assgTest2.c: In function 'Integrate':
assgTest2.c:12: warning: initialization makes pointer from integer without a cast
assgTest2.c:15: error: expected ';' before ')' token

I've had a look around and haven't been able to make sense of the answer to similar questions, any help would be appreciated.

1    void SamplePoint(double *point, double *lo, double *hi, int dim)
2    {
3       int i = 0;
4       for (i = 0; i < dim; i++)
5          point[i] = lo[i] + rand() * (hi[i] - lo[i]);
6    }
7
8    double Integrate(double (*f)(double *, int), double *lo, double *hi, int dim, 
9                     double N)
10    {
11       double * point = alloc_vector(dim);
12       double sum = 0.0, sumsq = 0.0;
13
14       int i = 0;
15       for (i = 0.0, i < N; i++)
16       {
17         SamplePoint(point, lo, hi, dim);
18
19         double fx = f(point, dim);
20         sum += fx;
21         sumsq += fx * fx;
22       }
23
24       double volume = 1.0;
25       i = 0;
26       for (i = 0; i < dim; i++)
27         volume *= (hi[i] - lo[i]);
28
29       free_vector(point, dim);
30       return volume * sum / N;
31    }

Edit: Fixed some mistakes, still giving the same error

2

1 Answer 1

9

I guess this is your line 12

    double * point = alloc_vector(dim);

The text of the warning is

warning: initialization makes pointer from integer without a cast

What this means is that the integer returned from alloc_vector() is being automatically converted to a pointer and you shouldn't do that (also you should not cast despite what the warning hints at).

Correction: add the proper #include where alloc_vector() is declared so that the compiler knows it returns a pointer and doesn't need to guess (incorrectly) it returns an integer.

Or, if you don't have the include file, add the prototype yourself at the top of your file

double *alloc_vector(int); // just guessing

line 15

     for (i = 0.0, i < N; i++)

The text for the error is

assgTest2.c:15: error: expected ';' before ')' token

Each for statement has two semicolons in the control structure (between parenthesis). Your control structure only has 1 semicolon. Change that to

     for (i = 0.0; i < N; i++)
     //          ^ <-- semicolon
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks heaps, that fixed the first error, however the "assgTest2.c:15: error: expected ';' before ')' token" error persists.
@user3422805: I've edited my post with the 2nd error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.