Structure
A structure is a user-defined and composite data type that makes a group of related variables. The variables declared in a structure, are known as members of the structure. Member variables of a structure can be of different types.
Memory allocation for a structure variable is equal to the sum of the memory requirement of each member variable of the structure.
In this context, in the following example, we have shown how a structure is defined and used in a program.
In this example, we have declared a structure variable Book with four member variables (book_id, title, author, and subject).
#include <stdio.h>
#include <string.h>
struct Book {
int book_id;
char title[50];
char author[50];
char subject[20];
};
int main()
{
struct Book b1;
b1.book_id = 6495407;
strcpy(b1.title, "Introduction to C Programming");
strcpy(b1.author, "Balaguruswamy");
strcpy(b1.subject, "C Programming");
printf("Book 1 book_id : %d\n", b1.book_id);
printf("Book 1 title : %s\n", b1.title);
printf("Book 1 author : %s\n", b1.author);
printf("Book 1 subject : %s\n", b1.subject);
return 0;
}
Array of Structure
In the above example in structure variable b1, we can store information of a book. But if we want to store many books information, we have to use an array of structures.
As we know that an array holds a similar type of data, in an array of structures we are not violating this rule. Each structure variables are homogenous though its members may be heterogenous.
An array of structures is declared as:
struct Book books[100];
Structure within Structure
#include <stdio.h>
typedef struct date
{
int day;
int mon;
int year;
}dt;
typedef struct employee
{
int empid;
char name[30];
char address[100];
char dept[10];
dt dob;
dt doj;
}emp;
int main()
{
emp e1;
printf("Enter the following information of the employee:");
printf("Name:");
scanf("%s", e1.name);
printf("Address:");
scanf("%s", e1.address);
printf("Department:");
scanf("%s", e1.dept);
printf("Date of Birth:");
scanf("%d", &e1.dob.day);
printf("Month of Birth:");
scanf("%d", &e1.dob.mon);
printf("Year of Birth:");
scanf("%d", &e1.dob.year);
printf("Date of Joining:");
scanf("%d", &e1.doj.day);
printf("Month of Joining:");
scanf("%d", &e1.doj.mon);
printf("Year of Joining:");
scanf("%d", &e1.doj.year);
printf("=========================================\n");
printf("Employee Information\n");
printf("name is %s\n", e1.name);
printf("address is %s\n", e1.address);
printf("department is %s\n", e1.dept);
printf("dob is %d/%d/%d\n", e1.dob.day, e1.dob.mon, e1.dob.year);
printf("doj is %d/%d/%d\n", e1.doj.day, e1.doj.mon, e1.doj.year);
return 0;
}
Union
Though unions are declared in a similar way as structures, unions are used for different purposes. In the case of structure, for each member, separate consecutive chunks of memory are allocated, whereas a piece of memory is allocated in a union with the size of the largest member. Hence, only one element can be stored at a time. If we allocate new data to a member, the previous data is overwritten.
Using a union, it's up to the developer to keep track of the data, which is stored by the program and the programmer should make sure that the right data is retrieved at the right time.
At this stage, it is very difficult to understand the actual application of unions. It will be discussed in the Data Structure tutorial.
In this context, the following example shows how to declare and use union, though it's not the real application.
#include <stdio.h>
#include <string.h>
union Book {
int book_id;
char title[50];
char author[50];
char subject[20];
};
int main()
{
union Book b1;
b1.book_id = 6495407;
strcpy(b1.title, "Introduction to C Programming");
strcpy(b1.author, "Balaguruswamy");
strcpy(b1.subject, "C Programming");
printf("Book 1 subject : %s\n", b1.subject);
return 0;
}
Here the problem is that more than one value can not be stored in book variable at a time. Hence, this is not the proper application of union.
So, the following example explains a real application of union.
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
union
{
float uf;
int ui;
} var;
var.uf = 50.5;
printf("value is %f\n", var.uf);
var.ui = 56;
printf("value is %d\n", var.ui);
exit(EXIT_SUCCESS);
}
In the example (shown above), the union variable, var, can hold either an integer or float data. And obviously, the storage allocation and data representation for an integer and float differ. Hence, you should be careful enough to store and retrieve proper data from the variable. In a union variable, each member is stored starting at the base address.
The most common way of using a union is to embed it in a structure, with another member of the structure used to indicate the type of the element currently stored in the union.
Here is how it might be used:
#include <stdio.h>
#include <stdlib.h>
/* code for types in union */
#define TYPE_FLOAT 1
#define TYPE_CHAR 2
#define TYPE_INT 3
struct var_data_type{
int type_in_union;
union {
float ufloat;
char uchar;
int uint;
} union_data;
} var_data_type;
void print(void)
{
switch(var_data_type.type_in_union)
{
default:
printf("Unknown type in union\n");
break;
case TYPE_FLOAT:
printf("%f\n", var_data_type.union_data.ufloat);
break;
case TYPE_CHAR:
printf("%c\n", var_data_type.union_data.uchar);
break;
case TYPE_INT:
printf("%d\n", var_data_type.union_data.uint);
break;
}
}
void main(void){
var_data_type.type_in_union = TYPE_FLOAT;
var_data_type.union_data.ufloat = 3.5;
print();
var_data_type.type_in_union = TYPE_CHAR;
var_data_type.union_data.uchar = 'a';
print();
exit(EXIT_SUCCESS);
}
Use of typedef
The keyword 'typedef' allows developers to define their own data type or primitive data types by a new data type name.
typedef struct student
{
int roll;
char name[20];
char address[100];
char dept[10];
} stud;
Hence instead of a variable using struct student std1
now, we can declare a variable as stud std1;
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.