Professional Documents
Culture Documents
Structure :
Structure declaration:
Structures must be declared before they are used. The general format for
declaring a structure is
Where,
struct is the keyword which tells the compiler that a structure is being
defined.
tag_name is the name of the structure.
member1, member2 … are called members of the structure which
may be variables, arrays, pointers or even other structures. The
members are declared within curly braces.
The closing brace must end with the semicolon.
Example:
Note:
1. The members of the structure do not occupy any memory until they are
associated with the structure variables.
Method 1:
The variables of the structure may be declared separately by using the structure
tag as follows
Method 2:
The structure variables can be specified after the closing brace when the
structure is declared.
Ex:
Note:
If other variables of the structure are not required, the tag_name can be omitted
as follows.
structurevariablename.membername;
Now each member of the structure variable s1 can be accessed using the dot(.)
operator as follows
structure Initialization:
The initial values for the components of the structure are placed in curly braces
and separated by commas. The members of the variable s1 i.e., rno, name, avg
are initialized to 1, “pavan” and 75.00 respectively.
struct student
int rno;
char name[80];
int marks[6];
int sum;
float per;
Example program:
#include <stdio.h>
struct student
int rollno;
char name[50];
int marks[6];
int sum;
float per;
};
int main()
int i,j,n;
clrscr();
scanf("%d",&n);
scanf("%d%s",&x[i].rollno,x[i].name);
C Programming Notes Page 6
Structures (Unit – VI)
for(j=0;j<6;j++)
scanf("%d",&x[i].marks[j]);
x[i].sum=0;
for(j=0;j<6;j++)
x[i].sum= x[i].sum+x[i].marks[j];
x[i].per=(float)(x[i].sum/6);
for(i=0;i<n;i++)
printf("%d\t\t%s\t\t\t%f\n",x[i].rollno,x[i].name,x[i].per);
In C, it is possible that structures of one type are made the members of another
structure. When a structure contains another structure, it is called nested
structure.
For example, suppose that we have two structures named student and date. To
make date nested to student, we have to define date structure before and outside
student structure and create an member of date structure inside student structure.
Here the structure student contains the variable of structure date. A variable of
struct student type thus can store rno, name, avg and date of admission(doa) of a
student. Now consider the following statement
struct student x;
Here x can represent details of a student with x.rno, x.name, x.avg and x.doa
respectively. Since x.doa becomes variable of struct date type x.doa.day,
x.doa.month and x.doa.year represent day, month and year part of doa.
where ptr1, ptr2, ......, ptrn etc. are pointers to structure variable
structurepointervariable → membername;
xp = &x;
will assign the address of the variable x to xp. the members of x can be
accessed using xp as
#include <stdio.h>
struct student
int rollno;
char name[80];
float per;
};
main()
ptr = &s1;
Structure may contain the Pointer variable as member. Pointers are used
to store the address of memory location. They can be de-
referenced by ‘*’ operator.
struct sample
int *ptr;
C Programming Notes Page 10
Structures (Unit – VI)
char *name;
}s1;
s1.ptr = #
s1.name = “pavan”;
printf(“name : %s”,s1.name);
output
Here in the declaration of the structure, name and author have been declared as
arrays. Therefore, when we call the function display( ) using, display ( b1.name,
b1.author, b1.callno ) ;
we are passing the base addresses of the arrays name and author, but the value
stored in callno. Thus, this is a mixed call—a call by reference as well as a call
by value.
output...
The whole structure is passed to another function by address. It means only the
address of the structure is passed to another function. The whole structure is not
passed to another function with all members and their values. So, this structure
can be accessed from called function by its address.
output...
union:
union unionname
{
datatype member-1;
datatype member-2;
………………….
………………….
datatype member-n;
};
union is a keyword and unionname is any valid identifier.
member-1,member-2, ………….,member-n are the members of the
union.
In case of a union, only one memory location which is large
enough to store largest data type member of the union gets
allocated. This single location can store values of different type
one at a time. But it can be referred by any one individual
member at any point of time.
union number
int i;
float f;
char c;
};
here n1 is a variable of type number union type. As a result, only one memory
location gets allocated and the size of the memory location is four bytes which
is equal to the size of the largest sized data type i.e., float in the memberlist.
#include <stdio.h>
#include <string.h>
union number
int i;
float f;
char c;
};
main( )
n1.i = 10;
n1.f = 220.5;
C Programming Notes Page 15
Structures (Unit – VI)
n1.c = ‘p’;
Output:
n1.i : 10
n1.f :220.500000
n1.c:p
Structure Union
1.The keyword struct is used to define 1. The keyword union is used to define a
a structure union.
2. In union, the total memory space
allocated is equal to the member with
2. In structure each member get largest size. All other members share the
separate space in memory. Take same memory space. This is the
below example. biggest difference between structure and
union.
struct student
union student
{
{
int rollno;
int rollno;
char gender;
char gender;
float marks;
float marks;
}s1;
}s1;
The total memory required to store a
structure variable is equal to the sum In above example variable marks is of
of size of all the members. In above float type and have largest size (4 bytes).
case 7 bytes (2+1+4) will be required So the total memory required to store
to store structure variable s1. union variable s1 is 4 bytes.
3. Each member within a structure is 3. Memory allocated is shared by
assigned unique storage area of individual members of union.
location.
4 Altering the value of a member will 4. Altering the value of any of the
not affect other members of the member will alter other member values.
structure.
5. Individual member can be accessed 5. Only one member can be accessed at a
at a time time.
6. Several members of a structure can 6. Only the first member of a union can
initialize at once. be initialized.
Bit fields
Integer fields occupy 16 bits to store data. There are some occasions
where data items require much less than 16 bits space. C Language permits us
to use small bit fields to hold data items and thereby to pack several data items
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.
struct {
};
Elements Description
width The number of bits in the bit-field. The width must be less than
or equal to the bit width of the specified type. The width is
decided by the range of values to be stored. The largest value
that can be stored is 2n-1, where n is width.
The variables defined with a predefined width are called bit fields. A bit
field can hold more than a single bit; for example, if you need a variable to store
a value from 0 to 7, then you can define a bit field with a width of 3 bits as
follows −
struct {
} Age;
The above structure definition instructs the C compiler that the age
variable is going to use only 3 bits to store the value. If you try to use more than
3 bits, then it will not allow you to do so.
The first field always starts with the first bit of the word.
Unsigned : bit-length
We cannot take the address of a bit field variable. This means we cannot
use scanf to read values into bit fields. We can neither use pointer to
access the bit fields.
Bit fields should be assigned values that are within the range of their size.
If we try to assign larger values, behaviour would be unpredicted.
struct {
} status;
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. For example, the above structure can be re-written as
follows −
struct {
} status;
The above structure requires 4 bytes of memory space for status variable,
but only 2 bits will be used to store the values.
If you will use up to 32 variables each one with a width of 1 bit, then also
the status structure will use 4 bytes. However as soon as you have 33 variables,
it will allocate the next slot of the memory and it will start using 8 bytes. Let us
check the following example to understand the concept −
#include <stdio.h>
#include <string.h>
struct {
} status1;
struct {
} status2;
int main( ) {
return 0;
When the above code is compiled and executed, it produces the following result
−
Example Program
#include <stdio.h>
#include <string.h>
struct {
} Age;
int main( ) {
Age.age = 4;
Age.age = 7;
Age.age = 8;
return 0;
When the above code is compiled it will compile with a warning and when
executed, it produces the following result −
Sizeof( Age ) : 4
Age.age : 4
Age.age : 7
Age.age : 0
C Programming Notes Page 21