如何将指针数组引用到几个const数组中

时间:2013-10-21 12:23:30

标签: c arrays pointers const

我有三组具有不同数据的数组

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中,我得到"警告:赋值从指针目标类型&#34中删除限定符;

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

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存储器中)?

什么是正确的方法?,如果可能,我更愿意将数据保存在常量内存中?

1 个答案:

答案 0 :(得分:2)

您的分析错误,pp不是const,但pp指向的值是const*pp是{{1} }})。

const

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

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

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

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