【问题标题】:Structure - Access Structure element without . and ->结构 - 没有 . 的访问结构元素。和->
【发布时间】:2016-11-01 02:46:00
【问题描述】:

我需要在不使用.-> 的情况下访问嵌套结构中的一些元素

我需要从测试笔记本电脑定义中打印出keyValuealternateKeyValue 的值,而不使用.-> 运算符直接引用qwerty 结构或其成员。

这是结构。

typedef struct
{
    bool leftButton;
    bool rightButton;
    bool middleButton;
    bool mouseOn;
    mouse_direction_E direction;
} mouse_S;

typedef struct
{
    char keyValue;
    char alternateKeyValue;
} keyboard_S;

typedef struct
{    
    mouse_S simpleMouse;
    keyboard_S qwerty;
} laptop_S;

laptop_S test=
{

    .simpleMouse =
    {
        .leftButton = false,
        .rightButton = false,
        .middleButton = false,
        .mouseOn = false,
        .direction = MOUSE_NONE,
    },
    .qwerty =
    {
        .keyValue = '5',
        .alternateKeyValue = '%'
    },
};

int main()
{

    char c = tesla.qwerty.keyValue;
    char d = tesla.qwerty.alternateKeyValue;
    printf("KeyValue = %c\n", c);
    printf("alternateKeyValue = %c\n", d);
}

这可行,但有没有办法在不使用“.”的情况下访问 KeyValuealternateKeyValue

【问题讨论】:

  • 下次请用CTRL+K缩进(选中代码块再CTRL+K)
  • 您可以使用offsetof() 和类型转换。但你为什么要这样做呢?

标签: c pointers struct


【解决方案1】:

可能他们希望你使用这样的东西:

union {
    mouse_S    mouse;
    keyboard_S keyboard;
    laptop_S   laptop;
} * oh; // oh = offset helper
size_t offset_of_mouse_leftButton = (char*)&oh->mouse->leftButton - (char*)&oh->mouse; // this should be 0
size_t offset_of_mouse_rightButton = (char*)&oh->mouse->rightButton - (char*)&oh->mouse; // but this one can be anything
size_t offset_of_mouse_middleButton = (char*)&oh->mouse->middleButton - (char*)&oh->mouse; // this too
// ...
size_t offset_of_keyboard_alternateKeyValue = (char*)&oh->keyboard->alternateKeyValue - (char*)&oh->keyboard;
// ...

然后用void *keyboard_S

int get_keyValue(void * _keyboard) {
    // usual way:
    // keyboard_S * keyboard = _keyboard;
    // return keyboard->keyValue;
    // requested way:
    return *(CHAR*)((char*)_keyboard + offset_of_keyboard_keyValue);
}

CHAR 类型应该小写,是元素keyValue 的类型。对于每种类型,其他char 必须是char,无论它是什么。 offset_of_ 变量定义中的上述chars 相同。

所以,我猜,剩下的就是你的功课了。

【讨论】:

    【解决方案2】:

    更直接和更简单的 3 行代码可能是...

    //Get the address of the tesla      
      char *addressoftesla  = (laptop_S*)(&tesla);
    
    //structure of tesla = simpleMouse + qwerty + 2 byte paddings
    
    /* Get the value at the starting address of qwerty i.e keyValue*/
      printf("keyValue of tesla keyboard is %c\n",*(addressoftesla+sizeof(mouse_S)));
    
    /* Get the value at the starting address of qwerty i.e alternatekeyValue */
      printf("alternatekeyValue of tesla keyboard is %c\n",*(addressoftesla+sizeof(mouse_S)+1));
    
      /*
      * More info for further understanding: 
      * A simple check to see all the values in the each field of structure
      * tesla - you can also use sizeof to get the each structure and entire 
      * structure byte sizes and the same can be done using offsetof as in 
      * other solution
      */
      for(int i = 0; i< 12; i++)
      {
          printf("value at tesla[i] is %d \n",*(addressoftesla+i));
      }
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 2014-09-14
      • 2019-08-10
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多