Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page >

構造体と共用体

構造体内のフィールドは、定義の順番に左右されることがあります。構造体のフィールドは、正しい順序で並べるか、フィールド名を使用して直接アクセスする必要があります。

バイト順序に左右される可能性のある構成要素が共用体に含まれている場合は、Listing 2-3に示すような形式を使用してください。wchを設定してからhiloをそれぞれwchの上位バイト、下位バイトとして読み取るコードは正しく動作します。逆の場合でも同じことがいえます。hiloを設定し、wchを読み取るコードは、どちらのアーキテクチャにおいても同じ値を得ます。別の例として、ヘッダファイル「IntlResources.h」に定義されているWideChar共用体を参照してください。

Listing 2-3  構成要素がバイト順序に左右される可能性のある共用体

union WChar{
    unsigned short wch;
    struct {
#if __BIG_ENDIAN__
        unsigned char hi;
        unsigned char lo;
#else
        unsigned char lo;
        unsigned char hi;
#endif
    } s;
}


< Previous PageNext Page >


Last updated: 2006-03-08




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice