麻烦与c语法 - const * const * definition

时间:2018-04-24 13:17:08

标签: c pointers syntax parameters const

使用如下定义的函数我遇到了麻烦:

some_function(address_t const * const * my_addrs, uint8_t length)

wheareas address_t定义为:

typedef struct
{
  uint8_t addr_id   : 1;       
  uint8_t addr_type : 7;       
  uint8_t addr[6]; 
} address_t;

我该如何调用此功能?

代码来自蓝牙库,应该设置蓝牙地址的白名单。因此,我们的想法是使用不同的addr [6]信息定义多个address_t结构。

非常感谢您的帮助

编辑:这里有更多信息

我有几个addres_t结构。它们的定义如下:

address_t addr1 = {.addr_id= 1, .addr_type = 3, .addr = {0x12,0x34,0x56,0x78,0x90,0xAB}};
address_t addr2 = ...

我可以将其组合成如下数组:

address_t my_whitelist[6];
my_whitelist[0] = addr1;
my_whitelist[1] = addr2;
...

我不确定是否需要这样做。现在我必须传递一些如何使用这个功能。我希望这些进一步的信息有所帮助。

1 个答案:

答案 0 :(得分:1)

  

我该如何调用此功能?

示例电话

import React from 'react';
import _ from 'lodash';
import base from '../base';

class Painting extends React.Component {
  constructor() {
    super()
    this.state = {
      pictures: []
    }
  }

  // firebase syncing
  componentWillMount() {
    this.ref = base.syncState('pictures', {
      context: this,
      state: 'pictures',
      asArray: true
    });
  }

  renderImage() {
    if (!this.state.pictures) {
      return ("Loading...");
    } else {
      return <p>{_.find(this.state.pictures, { slug: this.props.match.params.slug }).url}</p>
    }
  }


  render() {

    console.log(this.props.match.params.slug)

    return(
      <div>
        {this.renderImage()}
      </div>
    )
  }
}

export default Painting;

注意<span class="job-is ft" id="more" onclick="$('.details').slideToggle(function(){$('#more').html($('.details').is(':visible')?'Hide Phone':'Show Phone');});">Voir le numero</span> @if (Auth::check()) <div class="extra-job-info details" style="display:none"> <p style="text-align: center; padding: 10px; font-size: 45px;"><i class="la la-phone"></i> {{ $profiledetail->phone }}</p> </div> @else <p> In order to view this number you must login @endif 的类型更改。这需要是一个指针数组。由于上面的typedef struct { uint8_t addr_id :1; uint8_t addr_type :7; uint8_t addr[6]; } address_t; // 1st const, 2nd const // v---v v---v int some_function(address_t const * const * my_addrs, uint8_t length) { (void) my_addrs; (void) length; return 0; } int foo() { const address_t addr1 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } }; const address_t addr2 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } }; const address_t addr3 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } }; ,这些指针需要指向my_whitelist[]数据。

const

注意1st const not 需要 // address_t my_whitelist[6]; const address_t *my_whitelist[6]; my_whitelist[0] = &addr1; my_whitelist[1] = &addr2; my_whitelist[2] = &addr3; my_whitelist[3] = &addr1; my_whitelist[4] = &addr2; my_whitelist[5] = &addr1; uint8_t len = sizeof my_whitelist / sizeof my_whitelist[0]; ,因为上面的my_whitelist[]const一样。上面的2nd const通知调用代码const address_t * const my_whitelist[6];不会修改2nd const的数组元素。

some_function()

注意:如果my_whitelist[] return some_function(my_whitelist, len); } 数组,则其值不能分配,但可以初始化

my_whitelist[]

注意:const// Example usage with a `const my_whitelist[]` const address_t * const my_whitelist[] = { &addr1, &addr2, &addr3 }; 类似。 address_t const *前导符合C规范的风格。

const address_t *