Structures and Functions
Structures can be passed as an argument to a function and can be used by the functions for performing operations on them. They are 3 methods by which the values of the structure can be transferred from one function to another.
Method 1
The first method is to pass each member of a structure as actual
argument of the function call. The actual arguments are then
treated independently as ordinary variables.
Example:
#include<stdio.h> #include@lt;conio.h> void main() { struct stud { char name[20]; int age,no; }s; void display(char [],int,int); printf(“\n Enter name of student :”); gets(s.name); printf(“\n Enter Age and roll Num:”); scanf(“%d%d”,&s.age,&s.no); clrscr( ); display(s.name,s.age,s.no); getch( ); } void display(char ch[],int a, int n) { printf(“\n Name of student :%s”,ch); printf(“\n Age of Student :%d”,a); printf(“\n Roll Number :%d”,n); }
Method 2
In this method the entire structure is passed as an argument to
a function. Since only a copy of the structure is sent, any
changes made to structure members are not reflected int the
original structure. It is therefore necessary for the function
to return the entire structure back to the calling function.
General format of sending a copy of the structure is as follows
Functionname(Structure variable);
Example:
#include<stdio.h> #include@lt;conio.h> void main( ) { struct emp { char name[20]; int id; float sal; }; void display(struct emp) ; struct emp e; clrscr(); printf(“\n Enter name”); gets(e.name); printf(“\n Enter ID “); scanf(“%d”,&e.id); printf(“Enter salary :”); scanf(“%f”,&e.sal); display(e); getch(); } void display(struct emp e) { printf(“\n Employee name :%s”,e.ename); printf(“\n Employee id :%d”,e.id); printf(“\n Employee salary:%f”,e.sal); }
Method 3
In this method,the address of the structure is passed to the function. Any changes made to the structure members are reflected on to the original structure. This is because pointers deal directly with the data stored in the memory.
Bit Fields
We know that an integer occupies 2 bytes of memory which is equal to 16 bits. Most of the time, we don’t use all the 16 bits to store the integer. So there is a wastage of memory. To serve this purpose, C supports the concept of sharing data items packed in a word of memory.
A bit field is a set of adjacent bits whose size can be from 1 to 16 bits in length. A word can therefore be divided into a number of bit fields. The name and size of bit fields are defined using a structure. The general form of bit fields definition is:
struct tag_name { Data_type name1: bit_length; Data_type name2: bit_length; ………………………. ………………………. };
The data type can either be int or unsigned int or signed int
and the bit-length number bits used for specified name. The
bit-length is decided by the range of value to be stored. The
largest value that can stored is 2^n-1, where n is the
bit-length.
Note:
- Bit fields cannot be arrayed.
- There can unsed bits in a word.
- We cannot take the address of the bit-field variable. This means we cannot use scanf to read values into bit-fields. We can neither use pointers to access the bit-fields.