Sunday, February 1, 2009

Structures and Unions in c

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

struct mystruct
int numb;
char ch;

Structure has name mystruct and it contains two variables: an integer named numb and a character named ch.

struct mystruct s1;

Accessing Member Variables




typedef can also be used with structures. The following creates a new type sb which is of type struct chk and can be initialised as usual:
typedef struct chk
char name[50];
int magazinesize;
float calibre;
} sb;

ab arnies={"adam",30,7};

A union is an object that can hold any one of a set of named members. The members of the named set can be of any data type. Members are overlaid in storage. The storage allocated for a union is the storage required for the largest member of the union, plus any padding required for the union to end at a natural boundary of its strictest member.

union {
char n;
int age;
float weight;
} people;


For More Tutorials c tutorial

1 Comment:

Anonymous said...

thank it this helped a bit however you should prob change "ab arnies = {"adam",30,7};
to sb arnies={"adam", 30, 7};

if i understand correctly.