Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 2.50 average.
Old 23-Apr-2005, 13:04
pointer pointer is offline
Junior Member
Join Date: Apr 2005
Posts: 30
pointer is on a distinguished road

Difference between Structure and Union

What is the difference between structure and union? Where we use structure data type and where we use union data type?
Old 23-Apr-2005, 14:57
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
A structure is a collection of data. For example an employee record may contain
  • Name
  • address
  • hire date
  • salary
The structure could be defined
CPP / C++ / C Code:
    char   name[32];
    char   address[64];
    char   hireDate[10];
    double salary;

A union is the same location defined in different ways. For exxample
CPP / C++ / C Code:
    double dval;
    int    ival[2];
    char   cval[8];
will allow you enter a floating point number and display that number in it's raw integer or byte form. Below is a program I wrote a while back for just this purpose. It can also be used to define multiple definitions for the same structure. For example, if the first byte of each record is a record type, and each record type is a different format, you can read the entire record, test the type, and use values out of a union to process the data. If this is not clear, I'll put together a tutorial...

The following program is written for Borland. Change the getche() into an appropriate form for your compiler. I recommend the fgets() form. The if will also need a change.
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main()
    int ch;
    int i;
    char buf[50];
        double dd;
        float  ff[2];
        long   ll[2];
        int    ii[2];
        unsigned char   cc[8];
    } xx;

    printf("  D- double   \n");
    printf("  F- float    \n");
    printf("  L- long     \n");
    printf("  I- integer  \n");
    printf("  C- character\n");
    printf("  x- exit     \n");

    while (ch != 0x1B)
        xx.dd = 0.0L;
        printf("What would you like to enter?  ");
        ch = getche();
        ch = toupper(ch);
              ch = 0;

            case 'X':
              ch = 0x1B;
            case 0x1B:
            case 'D':
              printf("Enter a double: ");
              fgets(buf, 50, stdin);
              sscanf(buf, "%lf", &xx.dd);
            case 'F':
              printf("Enter a float: ");
              fgets(buf, 50, stdin);
              sscanf(buf, "%f", &xx.ff[0]);
            case 'L':
              printf("Enter a long: ");
              fgets(buf, 50, stdin);
              sscanf(buf, "%ld", &xx.ll[0]);
            case 'I':
              printf("Enter an int: ");
              fgets(buf, 50, stdin);
              sscanf(buf, "%d", &xx.ii[0]);
            case 'C':
              printf("Enter a char: ");
              fgets(buf, 50, stdin);
              sscanf(buf, "%c", &[0]);
        if (ch != 0x1B && ch != 0x00)
            printf("double: %15.5lf         \n", xx.dd);
            printf("float:  %15.5f   %15.5f \n", xx.ff[0], xx.ff[1]);
            printf("long:   %15ld   %15ld   \n", xx.ll[0], xx.ll[1]);
            printf("int:    %15d   %15d     \n", xx.ii[0], xx.ii[1]);
            printf("char:    "); 
                for (i=0; i < 8; i++) 
                    if (i == 4) printf("  ");
                    printf ("%02X  ",[i]); 
    return 0;

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:35.

vBulletin, Copyright © 2000 - 2018, Jelsoft Enterprises Ltd.