麻烦访问外部float数组 [英] trouble accessing external float array

查看:140
本文介绍了麻烦访问外部float数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件中有以下程序

I have the following program in two files

main.cpp

    float POW10[300];
    main(0
    {
        Fill_POW10();
    }


$ b b

Fill.cpp

Fill.cpp

extern float *POW10;
Fill_POW10()
{
  for(int i=0;i<300;i++)
  {
    POW10[i]=i;
  }
}

当我检查时,POW10是空的,但是如果我把Fill.cpp改成

This crashed with a segmentation fault. When I inspect, POW10 is NULL. However if I change Fill.cpp to

extern float POW10[];
Fill_POW10()
{
  for(int i=0;i<300;i++)
  {
    POW10[i]=i;
  }
}

代码工作正常,我认为POW10实际上是一个指针浮动,所以代码应该是相同的。你能解释为什么不是这样。

the code works fine. I was thinking that POW10 is actually implemented as a pointer to floats and so the codes should be identical. Can you please explain why this is not so.

推荐答案

首先阅读此条目解释您的问题:

First read this entry which explains your issue:

http://c-faq.com/aryptr/aryptr1.html

然后阅读此后续内容,说明数组和指针之间的差异。

Then read this follow up which explains the differences between array and pointer.

http://c-faq.com/aryptr/aryptr2.html

这篇关于麻烦访问外部float数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆