Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: initialization makes pointer from integer without a cast

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

like image 937
user3422805 Avatar asked Jan 12 '23 00:01

user3422805


1 Answers

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
like image 66
pmg Avatar answered Jan 13 '23 12:01

pmg