What is a union?

A union in C programming is a user defined variable which may hold members of different sizes and type. Union uses a single memory location to hold more than one variable. We can use the unions in the following locations.

  1. Share a single memory location for a variable myVar1 and use the same location for myVar2 of different data type when myVar1 is not required any more.
  2. Use it if you want to user, for example, a long variable as two short type variables.
  3. We don’t know what type of data is to be passed to a function, and you pass union which contains all the possible data types.

Defining a union in C

Union can be defined by the keyword union.

Here we have defined a union with the name myUnion and it has two members i.e. var1 of type int and var2 of type long

Declaring the union in C

We can declare the union in various ways. By taking the above example we can declare the above defined union as.

So newUnion will be the variable of type myUnion. We can also declare the union as

Initializing the union

We can initialize the union in various ways. For example

or we can initialize it as

In later stages we can also initialize the var2 as well but this will over write the var1 value. Normally when we declare the union it is allocated the memory that its biggest member can occupy. So here in our example newUnion will occupy the memory which a long type variable can occupy.

What is a structure?

A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.

A structure is combination of different data types. Lets take the example of a book, if we cant to declare a book we will be thinking about the name, title, authors and publisher of the book and publishing year. So to declare a book we need to have some complex data type which can deal with more than one data types. Lets declare a Book.

Defining a structure in C

We can declare a structure to accommodate the book.

The keyword struct defines a book, and each line with in the braces defines the elements of the Book. Now when ever we create an instance of Book it will have all the elements of the structure i.e. Name, Author, Publisher and Year.

Declaring a structure in C

We can declare the structure just like union.

Initializing a Structure in C

Lets initialize the structure variable CProgrammingBook.

Accessing the members of a Structure

We can access all the members of a structure by adding a period after the name of the structure variable name and then the name of the field we want to access. For example we want to change the Publish year from 2001 to 2002, we will do
it as

STRUCTURE

A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design. A structure is combination of different data types using the & operator, the beginning address of structure can be determined. This is variable is of type structure, then & variable represent the starting address of that variable.

STRUCTURE DEFINITION

A structure definition creates a format that may be used to declare structure variables consider the following example.

Here keyword Struct hold the details of four fields these fields are title, author, pages, and price, these fields are called structure elements. Each element may belong to different types of data. Here book-bank is the name of the structure and is called the structure tag. It simply describes as shown below.

Struct book-bank

Title array of 20 characters

Author array of 15 characters

Pages integer

Price float

The general format of a structure definition is as follows

ARRAY OF STRUCTURES

Each element of the array itself is a structure see the following example shown below. Here we want to store data of 5 persons for this purpose, we would be required to use 5 different structure variables, from sample1 to sample 5. To have 5 separate variable will be inconvenient.

The structure type person is having 2 elements: Name an array of 25 characters and character type variable age

USING THE STATEMENT

Struct person sample[5]; we are declaring a 5 element array of structures. Here, each element of sample is a separate structure of type person. We, then defined 2 variables into index and an array of 8 characters, info. Here, the first loop executes 5 times, with the value of index varying from 0 to 4. The first printf statement displays. Enter name gets( ) function waits for the input string. For the first time this name you enter will go to sample[0]. name. The second printf display age the number you type is will be 5 stored as character type, because the member age is declared as character type. The function atoi( ) converts this into an integer. atoi stands for alpha to integer. This will be store in sample[0] age. The second for loop in responsible for printing the information stored in the array of structures.

6 Shares
Share6
+1
Tweet
Share
Stumble