C语言 错误:初始化使指针从整数而不进行强制转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22422400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:53:40  来源:igfitidea点击:

Error: initialization makes pointer from integer without a cast

cpointerscasting

提问by user3422805

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

编辑:修正了一些错误,仍然给出同样的错误

回答by pmg

I guess this is your line 12

我猜这是你的第 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).

这意味着从返回的整数将alloc_vector()自动转换为指针,您不应该这样做(尽管警告提示,您也不应该强制转换)。

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

更正:添加正确的#includewherealloc_vector()声明,以便编译器知道它返回一个指针并且不需要猜测(错误地)它返回一个整数。

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

第 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 语句在控制结构中都有两个分号(在括号之间)。您的控制结构只有 1 个分号。将其更改为

     for (i = 0.0; i < N; i++)
     //          ^ <-- semicolon