如何声明一个常量"可变指针"到一个不可变的内存块

时间:2014-09-29 21:49:09

标签: c c99 const-correctness

我想知道如何在C99中声明(a const pointer to (a mutable pointer to (a const type)))

我们说我有这个电话网站:

const uint8_t* result;
create(&result);

void create(const uint8_t * const * resultPtr)是否是在这种情况下声明被调用者的正确方法,还是意味着无意的?我不需要重新分配resultPtr,调用者也不应该[从这个角度来看] *resultPtr[0],但我的功能需要指定*resultPtr = …

2 个答案:

答案 0 :(得分:4)

我们问cdecl

$ cdecl declare p as const pointer to pointer to const char
const char ** const p

此声明可以从右到左完全阅读以获得英文版本(记住const charchar const是相同的)。

答案 1 :(得分:1)

一些经验法则:

  • indexer = 0 for i in df1.columns: # pulls the column name from the above df for j in df1[i].iteritems(): #pulls the tagid from that row in the df if i == list(ctrl_data.keys())[indexer] and j[0] == order[0]: # just checks that the loop is iterating correctly print(j[1]) print(i) ctrl_data[i]['time_stamp'] = values_pull_process(j[1],start1,end1,num_points,token,base_uri)[0] ctrl_data[i][order[0]].clear() print(ctrl_data[i][order[0]]) ctrl_data[i][order[0]] = values_pull_process(j[1],start1,end1,num_points,token,base_uri)[1] print(ctrl_data[i][order[0]]) elif i == list(ctrl_data.keys())[indexer] and j[0] == order[1]: ctrl_data[i][order[1]] = values_pull_process(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[2]: ctrl_data[i][order[2]] = values_pull_process(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[3]: if j[1] == 'NaN': ctrl_data[i][order[3]] = j[1] else: ctrl_data[i][order[3]] = values_pull_ctrl(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[4]: if j[1] == 'NaN': ctrl_data[i][order[4]] = j[1] else: ctrl_data[i][order[4]] = values_pull_ctrl(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[5]: if j[1] == 'NaN': ctrl_data[i][order[5]] = j[1] else: ctrl_data[i][order[5]] = values_pull_ctrl(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[6]: if j[1] == 'NaN': ctrl_data[i][order[6]] = j[1] else: ctrl_data[i][order[6]] = values_pull_process(j[1],start1,end1,num_points,token,base_uri)[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[7]: ctrl_data[i][order[7]] = j[1] elif i == list(ctrl_data.keys())[indexer] and j[0] == order[8]: ctrl_data[i][order[8]] = j[1] else: print('Unable to find data for loop: {}, type: {}, tag_id: {}'.format(i,j[0],j[1])) print('') indexer = indexer + 1 始终创建指向<type> * const <varName> = <value>内容的不变指针<varName>。请考虑星号<type>右侧的const
  • 所有这些选项创建不可变的内容:
    • * const
    • const <type> <pointers> <varName> = <value>
    • <type> const <pointers> <varName> = <value>
    • const <type> const <pointers> <varName> = <value>

注意:最后两个选项返回

警告:重复的'const'声明说明符[-Wduplicate-decl-specifier]

在Clang编译器上,应该被视为不良做法。

因此,要使指向不变的指针的不可变指针指向<type> const const <pointers> <varName> = <value>,所有这些选项都起作用:

  • uint8_t
  • const uint8_t * * const result = NULL
  • uint8_t const * * const result = NULL
  • const uint8_t const * * const result = NULL

但是,第二个选项

uint8_t const const * * const result = NULL

似乎是恕我直言的最易读和规范的方法。您可能会发现this post在这方面也很有帮助。