EmbLogic's Blog

structures

structure is a user defined data type available in c programming,it allows us to combine data items of different kinds and helps us to construct a complex data type in a meaningful way.

Structure is used to represent a record. Suppose we want to keep track of books in a library. For this we have to track some attributed(title,author,subject,book id) of each book. To define a structure we must use the struct statement. This statement defines a new data type with more then one member for our program . the format for struct statement is :

struct [structure tag]

{

member definition;

member definition;

….

}[one or more structure variables];

the structure tag and structure variable is optional and each member definition is a normal variable definition such as int i; or float j or any other variable definition we can also declare structure variable separately like:

struct structure tag structure variable1,structure variable2… ;

a structure member has no meaning on its own so we cannot access it directly. In order to access a structure member,the member name must be linked with the structure variable using dot (.)operator also called period or member access operator .we can also use structure pointer operator if we are declaring a pointer variable for that structure .

Structures can also be nested within other structure in C programming and they can also be passed as a function argument like any other variable.

Bit fields:-

bit fields allow the packing of data in a structure. This is specially useful when memory is limited . Suppose our structure contains some true/false variables then we are going to store either 0 or 1 in these variables. so C programming offers us a better way to utilize the memory space in these situations . We can define the width of variable which tells the C compiler that we are going to use only those number of bits for example :

struct

{

unsigned int cgpa : 3;

}student;

the above structure definition instructs C compiler that cgpa variable is going to use only

3 bits to store the value,if we try to use more then 3 bits then it will not allow us to do so.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>