[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Skillnaden mellan struct och union?



Jeg antager at en int er 16 bit:

struct some_struct
    {
    int a;
    char b;
    char c;
    }
assembly
    org start_of_struct
    db    sizeof(int)    ;a
    db    sizeof(char)   ;b
    db    sizeof(char)   ;c

union some_union
    {
    int a;
    struct some_struct_in_some_union
        {
        char b;
        char c;
        }
    }
assembly
    org start_of_union
    db    sizeof(int)    ;a
    org start_of_union   ;start_of_some_struct_in_some_union
    db    sizeof(char)   ;b
    db    sizeof(char)   ;c

Som det kan ses af ovenstående er den store forskel mellem en struct og en
union måden data gemmes.
I en struktur ligger alle data sekventielt efter hinanden, så der kan
læses/skrives til alle data.
I en union ligger data "oven i" hinanden. Det er på den måde muligt at gemme
data i et format, og læse dem ud i et andet.
Jeg bruger oftest dette til at lave ind-/udlæsning af data til/fra filer.
Jeg læser data ind i en union som char[], og udlæser i den form jeg har brug
for det. Til dette ser min union således ud:
union tConvert
    {
    char cvChar;    // A char to be read
    int  cvInt;     // An integer to be read
    long cvLong;    // A long int to be read
    float cvFloat;  // A float to be read
    double cvDouble; // A double precision float to be read (largest type in
the union)
    char  cvIOarray[sizeof(double)]; // A string to be read, doubles as I/O
area for the function(s)
    }

Håber dette hjælper til forståelsen.

Keld Laursen

Kalle Andersson skrev i meddelelsen ...
Hejsan!

Kan någon förklara skillnaden mellan en struktur variabel och en union
variabel?

struct siffror { int a; double b; long c; }
struct siffror tal1;

union siffror { int a; double b; long c; }
union siffror tal1;

Det verkar innebära precis samma sak!?

--
Med vänlig hälsning
Kalle Andersson
sslug@sslug







 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *