【问题标题】:How to reference pointer array to several const array如何将指针数组引用到几个 const 数组
【发布时间】:2013-10-29 22:32:30
【问题描述】:

我有三组不同数据的数组

const UINT16 array1[4] = {1,2,3,4};   

const UINT16 array2[4] = {3,2,2,4};

const UINT16 array3[4] = {8,7,2,4};   //This is not PIN code, :-)

...

void example(void)
{

    UINT16 * pp;

    UINT16 data;

    pp = array1;

    data = pp[0];

    pp = array2;

    data = pp[3];

    pp = array3;

    data = pp[2];

    //and rest of code, this is snipped version of my larger code

}

在 dspIC33 中,我收到“警告:赋值丢弃来自指针目标类型的限定符”

根据谷歌搜索的印象,我可能会这样做......

void example(void)
{

    const UINT16 * pp;

    pp = array1;

    pp = array2;

    pp = array3;

    //and rest of code, this is snipped version of my larger code

}

那么这会使存储地址数据的 pp 变量成为固定值吗? (即在 ROM 内存中)?

什么是正确的方法?如果可能的话,我更喜欢将数据保存在 const 内存中?

【问题讨论】:

    标签: c arrays pointers constants


    【解决方案1】:

    你的分析错了,pp 不是constpp 指向的值是const(即*ppconst)。

    const UINT16 * pp; // means pp is a pointer to a const UINT16 value
    

    如果你希望 pp 是 const ,因为指向的地址是 const 你必须写:

    UINT16 * const pp; // means pp is a const pointer to a UINT16 value
    

    如果你想同时拥有指针和指向值的常量,你必须写:

    const UINT16 * const pp; // means pp is a const pointer to a const UINT16 value.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 2021-12-08
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      相关资源
      最近更新 更多