2016-11-01 34 views
0

私は.->私が直接参照するために.または->演算子を使用せずにテストラップトップの定義から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にアクセスする方法がありますか「」?

+0

、あなたがoffsetofは '使用することができますインデント(コードブロックを選択し、CTRL + K)する –

+4

をCTRL + Kを使用してください次回() 'とタイプキャストします。しかし、なぜあなたはこれを行う必要がありますか? – Barmar

答えて

2

おそらく彼らはあなたがこのようなものを使用したい:keyboard_Sからvoid *

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; 
// ... 

、その後:

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でなければなりません。前述のcharの変数定義のと同じです。

だから、残りはあなたの宿題だと思います。

1

より簡単でシンプルなコードは可能性が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)); 
    } 
関連する問題