A structure is a collection of one or more variables(possibly of
different types)under a single name. For instance, you want to store the
information about a student like roll no, class, marks etc. The one way
to do so is declaring variables separately But C offers Structures to
handle such situations under one roof. Thus,it can be said that A
structure is a collection of variables referenced under one name.
Syntax of structure :
struct first {
/*member element declarations */
}
The keyword struct tells the compiler that a structure is being defined. An optional name called a structure tag may follow the struct(as with first here).
Let's take a example:
struct student{
int roll_no;
char name;
int class;
float marks;
}
In the above definition, the student is a structure tag.At this point of time(i.e. after the above definition), no structure variable has been declared, that is, no memory has been reserved. So a structure declaration that is not followed by a list of variables reserves no storage.
Structure variable declaration :
Now after defining structure , we need to declare structure variable.
To declare a structure variable having the data form as defined by student, we write
student senior_student;
Now,
struct student{
int roll_no;
char name;
int class;
float marks;
}student senior_student;
structure senior_student will be having its elements as roll_no , name , class etc.
Alternatively we can write
struct student{
int roll_no;
char name;
int class;
float marks;
}senior_student;
Both have same meaning.
Referencing structure elements:
There are two types of operators used for accessing members of a structure:
1. Member operator( . )
2. Structure pointer operator( -> )
General syntax for access structure element by memory operator is :
structure_name.member;
Suppose, we want to access name for variable senior_student. Then, it can be accessed as:
senior_student.name;
The Structure pointer operator( -> ) will discuss in pointer section.
Structure and functions:
Structure can be passed to functions by value as well as by reference just like other variable.
Typedef :
C provides a facility called typedef for creating new data type names
For example, the declaration
typedef int student;
makes the name student a synonym for int. The type student can be used in declaration casts etc., in exactly same way that the type int can be:
student roll_no, marks;
difference between typedef and #define :
typedef is similar to #define(The #define is a C-directive which is also used to define the aliases for various data types), except that typedef is interpreted by the compiler where as #define statements are processed by the pre-processor.
Unions:
A union is a special data type available in C that enables you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.
The syntax is based on structures:
union u_tag{
int val;
float fval;
char cval[20];
}u;
The u_tag is optional. Now, a variable of u_tag type can store an integer, a floating-point number, or a string of characters. This means that a single variable i.e. same memory location can be used to store multiple types of data.
The memory occupied by a union will be large enough to hold the largest member of the union. For example, in above example u_tag type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by character string.
let's see:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
it produces the following result:
Memory size occupied by data : 20
Syntactically, members of a union are accessed as
union_name.member
or
union_name->member
just as for structures.
union may occur within structures and arrays, and vice versa.
Difference between union and structure:
#include <stdio.h>
union student_union {
char name[32];
float marks;
int roll_no;
}u;
struct student_struct {
char name[32];
float marks;
int roll_no;
}s;
int main(){
printf("size of union = %d \n",sizeof(u));
printf("size of structure = %d", sizeof(s));
return 0;
}
When the above code is compiled and executed, it produces the following result:
As you can see,There is difference in memory allocation between union and structure
The amount of memory required to store a structure variables is the sum of memory size of all members:
name+marks+roll_no = 32+4+4 = 40 bytes
But, the memory required to store a union variable is the memory required for largest element of an union:
name = 32 bytes
Another difference is, all members of structure can be accessed at any time. But, only one member of union can be accessed at a time in case of union and other members will contain garbage value.
Bit Fields :
When storage is at a premium, it may be necessary to pack several objects into a single machine word i.e. 0 or 1 .The C programming language offers a better way to utilize the memory space in such situation.
Suppose a C program contains a number of TRUE/FALSE variables grouped in a structure called status, as follows:
struct
{
unsigned int width;
unsigned int height;
} status;
This structure requires 8 bytes of memory space but in actual we are going to store either 0 or 1 in each of the variables. If you are using such variables inside a structure then you can define the width of a variable which tells the C compiler that you are going to use only those number of bytes.
More detail Bit Field .
If you like this post or have any question, please feel free to comment !
Syntax of structure :
struct first {
/*member element declarations */
}
The keyword struct tells the compiler that a structure is being defined. An optional name called a structure tag may follow the struct(as with first here).
Let's take a example:
struct student{
int roll_no;
char name;
int class;
float marks;
}
In the above definition, the student is a structure tag.At this point of time(i.e. after the above definition), no structure variable has been declared, that is, no memory has been reserved. So a structure declaration that is not followed by a list of variables reserves no storage.
Structure variable declaration :
Now after defining structure , we need to declare structure variable.
To declare a structure variable having the data form as defined by student, we write
student senior_student;
Now,
struct student{
int roll_no;
char name;
int class;
float marks;
}student senior_student;
structure senior_student will be having its elements as roll_no , name , class etc.
Alternatively we can write
struct student{
int roll_no;
char name;
int class;
float marks;
}senior_student;
Both have same meaning.
Referencing structure elements:
There are two types of operators used for accessing members of a structure:
1. Member operator( . )
2. Structure pointer operator( -> )
General syntax for access structure element by memory operator is :
structure_name.member;
Suppose, we want to access name for variable senior_student. Then, it can be accessed as:
senior_student.name;
The Structure pointer operator( -> ) will discuss in pointer section.
Structure and functions:
Structure can be passed to functions by value as well as by reference just like other variable.
Typedef :
C provides a facility called typedef for creating new data type names
For example, the declaration
typedef int student;
makes the name student a synonym for int. The type student can be used in declaration casts etc., in exactly same way that the type int can be:
student roll_no, marks;
difference between typedef and #define :
typedef is similar to #define(The #define is a C-directive which is also used to define the aliases for various data types), except that typedef is interpreted by the compiler where as #define statements are processed by the pre-processor.
Unions:
A union is a special data type available in C that enables you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.
The syntax is based on structures:
union u_tag{
int val;
float fval;
char cval[20];
}u;
The u_tag is optional. Now, a variable of u_tag type can store an integer, a floating-point number, or a string of characters. This means that a single variable i.e. same memory location can be used to store multiple types of data.
The memory occupied by a union will be large enough to hold the largest member of the union. For example, in above example u_tag type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by character string.
let's see:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
it produces the following result:
Memory size occupied by data : 20
Syntactically, members of a union are accessed as
union_name.member
or
union_name->member
just as for structures.
union may occur within structures and arrays, and vice versa.
Difference between union and structure:
#include <stdio.h>
union student_union {
char name[32];
float marks;
int roll_no;
}u;
struct student_struct {
char name[32];
float marks;
int roll_no;
}s;
int main(){
printf("size of union = %d \n",sizeof(u));
printf("size of structure = %d", sizeof(s));
return 0;
}
When the above code is compiled and executed, it produces the following result:
As you can see,There is difference in memory allocation between union and structure
The amount of memory required to store a structure variables is the sum of memory size of all members:
name+marks+roll_no = 32+4+4 = 40 bytes
But, the memory required to store a union variable is the memory required for largest element of an union:
name = 32 bytes
Another difference is, all members of structure can be accessed at any time. But, only one member of union can be accessed at a time in case of union and other members will contain garbage value.
Bit Fields :
When storage is at a premium, it may be necessary to pack several objects into a single machine word i.e. 0 or 1 .The C programming language offers a better way to utilize the memory space in such situation.
Suppose a C program contains a number of TRUE/FALSE variables grouped in a structure called status, as follows:
struct
{
unsigned int width;
unsigned int height;
} status;
This structure requires 8 bytes of memory space but in actual we are going to store either 0 or 1 in each of the variables. If you are using such variables inside a structure then you can define the width of a variable which tells the C compiler that you are going to use only those number of bytes.
More detail Bit Field .
If you like this post or have any question, please feel free to comment !
No comments:
Post a Comment