【问题标题】:Error: initialization makes pointer from integer without a cast错误:初始化使指针从整数而不进行强制转换
【发布时间】:2014-03-15 10:13:29
【问题描述】:

这段代码返回错误时遇到问题:

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

我环顾四周,无法理解类似问题的答案,我们将不胜感激。

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    }

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

【问题讨论】:

标签: c pointers casting


【解决方案1】:

我猜这是你的第 12 行

    double * point = alloc_vector(dim);

警告的文字是

warning: initialization makes pointer from integer without a cast

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

更正:在声明 alloc_vector() 的地方添加正确的 #include,以便编译器知道它返回一个指针,而无需猜测(错误地)它返回一个整数。 p>

或者,如果您没有包含文件,请自己将原型添加到文件顶部

double *alloc_vector(int); // just guessing

第 15 行

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

错误的文本是

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

每个 for 语句在控制结构中都有两个分号(括号之间)。您的控制结构只有 1 个分号。将其更改为

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

【讨论】:

  • 感谢堆,修复了第一个错误,但是“assgTest2.c:15: error: expected ';'在')'令牌之前"错误仍然存​​在。
  • @user3422805:我已经用第二个错误编辑了我的帖子。
猜你喜欢
  • 2011-05-10
  • 2012-10-30
  • 2013-09-07
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
相关资源
最近更新 更多