【问题标题】:Re assigning pointers重新分配指针
【发布时间】:2020-09-27 19:29:16
【问题描述】:

目前,我有一些 Arduino 代码让我发疯,我希望你能提供帮助。 我想要做的是初始化一个指向两个无符号整数数组之一的指针。 每次传递都会读取一个开关并相应地分配指针,只有编译器对分配有一个杂乱无章的配合。

unsigned int spl_add_tbl[4096];   // arrary table no zero crossing points. 
unsigned int zxd_add_tbl[4096];   // array table for zero crossing points. 
unsigned int *tbl_ptr;  // init table pointer
  
// ZXD switch D64. H = off, L = on.
if (digitalRead(64) == 1) {
  // Zero Crossing off, assign Table Pointer to sample table
  *tbl_ptr = &spl_add_tbl[0]; 
} else {
  // Zero Crossing on, assign Table pointer to Zero Crossing Table and set A/D read mapping.
  *tbl_ptr = &zxd_add_tbl[0]; 
}

编译器给出错误:从'unsigned int*'到'unsigned int'的无效转换[-fpermissive]

【问题讨论】:

  • tbl_ptr 是指向 unsigned int 的指针。 *tbl_ptrunsigned int。不要将指向unsigned int 的指针分配给unsigned int
  • 干杯埃里克,但我不确定我是否完全理解。

标签: pointers arduino


【解决方案1】:

tbl_ptr 是一个指针 - 你不应该为了给它分配一个数组而取消引用它:

if(digitalRead(64) == 1)
{
    // Zero Crossing off, assign Table Pointer to sample table
    tbl_ptr = &spl_add_tbl[0]; 
    
}
else 
{
    // Zero Crossing on, assign Table pointer to Zero Crossing Table and set A/D read mapping.
    tbl_ptr = &zxd_add_tbl[0]; 
}

【讨论】:

  • 干杯 Mureinik,这已经成功了,你保持了我的理智。我可以问一下,* 不是取消引用吗?。
  • @jlpniewski Damnit,把典型的词“不”放在那里。对不起这是我的错。 (不过,代码本身不应该没有改变)
  • tbl_ptr = &zxd_add_tbl[0];可能只是 tbl_ptr = zxd_add_tbl;当你放下括号时,你有一个指向数组第一个元素的指针。无需取消引用括号,然后再次获取地址。
猜你喜欢
  • 2013-01-07
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多