0% found this document useful (0 votes)
12 views

structures

Uploaded by

Anchal sandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

structures

Uploaded by

Anchal sandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1

STRUCTURE
In basic terms we refer to structure as a collection of heterogeneous data elements. Structures are
basically used to collect information related to single entity as one collective unit. We group a number
of variables of heterogeneous data types under one structure which then can be accessed through the
objects of that structure. They act like classes with all members defined with default public access
specifier. Syntax struct struct_name
{
type1 member1; type2 member2; typen membern;
} obj1, obj2;
In C, struct keyword is used to declare a structure followed by the name of the structure. This name
must be defined if we would like to make multiple copies of our structure just like any other data type
in C programming language. Simple objects or array of objects can also be declared for creating
instances of structure.
Example
struct student
{ char name[30]; int age; int rollno; float marks;
} s1, s2;
In this case, we have declared a structure bearing “student” name. In this structure we have declared 4
members inside the body. Total of 2 objects have also been created namely “s1 and s2”.

MEMORY ALLOCATED TO THE STRUCTURE


struct student
{ char name[30]; 30 + 2+2+2 int age; int rollno; int marks;
};
Student s1;
As such memory is not allocated to a structure, and when we create object of the structure, memory is
allocated to that object. Memory allocated to the object is equivalent to the total memory taken by all
the members of the structure together. In the above example memory allocated will be as follows:-
Name Age Rollno Marks
30 bytes 2 bytes 2 bytes 2 bytes
Table 8.1 Memory allocated to structure
So total memory allocated to the structure s1 will be 30+2+2+2= 36 bytes. In case of self referenced
structure, memory allocated to the pointer of the structure object will always be 2 bytes. Explanation of
the self referenced structure has been given later in this chapter.

DECLARING OBJECTS OF A STRUCTURE


When we create a structure, no memory is allocated to it. As we create objects of that structure, memory
is allocated to the objects.
2

Different ways to create objects of a structure:-


1. struct student
{ char name[30]; int age; int rollno;
}s1, s2; // objects of the structure declared here
In this case two objects of the structure student have been declared. We just need to mention the names
of the objects separated by commas before the ‘;' to successfully create objects of that structure. }s1, s2;
// objects of the structure declared here

2. struct student
{ char name[30]; int age; int rollno;
}; student s1,s2; // objects of the structure declared here Second way to create objects of the structure
is to first create the structure and then later on you can create the objects of that structure just like
declaring any other simple variables.
3.
struct student
{ char name[30]; int age; int rollno;
};
student s1{“dinesh”,27,101}; // object declared here
In this case as you can see that while creating the object of that structure we have also initialized the
members of the structure.
Now there are three data members of the structure student. As we initialize the members of the objects,
following values will be assigned to each member.
Name Dinesh
Age 27
Rollno 101
Table 8.2 Initialization of structure

ACCESSING MEMBERS OF THE STRUCTURE


Once the object of a structure has been declared, we are now ready to access the members of that
structure and make changes into its value.
struct student
{ char name[30]; int age; int rollno; }obj1, obj2;
When we wish to access any member the structure, following code of syntax must be used to do so.
3

obj1.age

Structure object Membership operator Member name

Accessing structure members

As we can see that there are three members of the structure student. When we create 2 objects of that
structure, separate copy of all the three members will be allocated to each object. Explanation obj1.age
= 27; obj2.age = 28;
You can see that we are trying to assign value to the member age twice. But the value will not be
overwritten as member age belongs to two different objects and hence the memory allocated to them is
also separate.

Difference between Structure and Array


ARRAY STRUCTURE

Structure refers to a collection

Array refers to a collection consisting of consisting of elements of

elements of homogenous data type. heterogenous data type.

Array uses subscripts or “[ ]” (square Structure uses “.” (Dot operator) for

bracket) for element access element access

Array is pointer as it points to the first

element of the collection. Structure is not a pointer

Instantiation of Array objects is not Instantiation of Structure objects is

possible. possible.
4

ARRAY STRUCTURE

Array size is fixed and is basically the Structure size is not fixed as each

number of elements multiplied by the element of Structure can be of

size of an element. different type and size.

Bit filed is not possible in an Array. Bit filed is possible in an Structure.

Array declaration is done simply using [] Structure declaration is done with

and not any keyword. the help of “struct” keyword.

Structure is a user-defined

Arrays is a primitive datatype datatype.

Array traversal and searching is easy Structure traversal and searching is

and fast. complex and slow.

struct sruct_name{

data_type1 ele1;

data_type2 ele2;

data_type array_name[size]; };

Structure elements may or may not

Array elements are stored in continuous be stored in a continuous memory

memory locations. location.

Array elements are accessed by their Structure elements are accessed

index number using subscripts. by their names using dot operator.


5

8.8 IMPLEMENTATION OF STRUCTURE

To input and output the contents of student structure


#include<stdio.h> #include<conio.h> struct student
{
int rollno;
char name[30];
int age;
}obj1; main( )
{
/* read values of structure members from user */ printf("Enter Roll No: ");
scanf(“%d”,&obj1.rollno); printf("Enter Student name: "); scanf(“%s”,&obj1.name);
printf("Enter age: "); scanf(“%d”,&obj1.age);
printf("Details of the structure are:\n");
printf("roll no : %d name: %s age: %d”,obj1.rollno, obj1.name, obj1.age);
}
8.12 ARRAYS OF STRUCTURES
Now that we are familiar with structures and how can we can create and access it, we need to learn
variety of ways to make the use of structures more useful. It is not a problem to create one or two objects
of the structure, but it really gets messy when we need to create them in plenty. One easy solution to this
problem is to create array of structures and access its multiple objects using index value. Example
struct student
{ char name[30]; int age; int rollno;
}obj[3];
Program 8.6. To create array of structure
#include<conio.h> #include<stdio.h>
struct student
{
char name[30]; int age; int rollno;
}obj[3]; // array of objects main( )
{ int i;
printf("Enter details of students\n"); for(i=0;i<3;i++)
{
printf("Enter name : "); scanf(“%s”,&obj[i].name); printf("Enter age : ");
scanf(“%d”,&obj[i].age); printf("Enter roll no : ");
scanf(“%d”,&obj[i].rollno);
}
printf("Details of students\n"); // output details of all students for(i=0;i<3;i++)
{
printf("\nRoll No: %d name : %s age : %d",obj[i].rollno,obj[i].name,obj[i].age) ;
}
}

UNIONS
Union is another user defined data type like structure which is capable of grouping together members of
heterogeneous data type under one name. Beside many similarities between structure and union, there
6

are operational differences between the two. All of the members of a union share the same memory
space.
After declaring a union object, instead of allocating memory for each member variable, the compiler
assigns an amount of memory equal to the largest variable. Example of structure, structure citizen
{ char name[30]; char idno[20];
};
Memory allocated to the structure is as follows:-
Name Idno Total
30 bytes 20 bytes 50 bytes
Table 8.3 memory allocated to structure Example of union, union citizen // keyword union
to declare union
{ char name[30]; char idno[20];
};
Memory allocated to the union is as follows:-
Name Idno Total
30 bytes - 30 bytes

Table 8.4 memory allocated to union


In some applications, we might want to access information of one of two alternate forms. Suppose, we
wish to store information about a citizen, and the citizen may be identified either by name or by an
identification number, but never both at the same time. If we define a structure which has both fields, it
will result into wastage of memory for both fields. C provides a data structure which fits our needs in
this case called a union data type. The declaration of a union type must specify all the possible different
types that may be stored in the variable. For example, we can declare a union variable, citizen, with two
members.
program 8.9. To implement union
#include<stdio.h> #include<conio.h>
union citizen // keyword union to declare union
{
char name[30]; char idno[20]; }obj1; main( )
{
printf("enter name : "); scanf(“%s”,&obj1.name); printf("Enter idno : ");
scanf(“%s”,&obj1.idno);
printf("name : %s idno : %s",obj1.name, obj1.idno);
}

8.16 DIFFERENCE BETWEEN UNION AND STRUCTURE


UNION Structure
7

1. The union keyword is used to declare a union. The struct keyword is used to declare a
structure.
2. Memory allocated to union is equal to the
memory that’s needs to be allocated to its
Members of the structure are allocated
largest member.
memory depending upon their data type, so
memory allocated is flexible. You don’t have
to declare the objects of structure at the time of
3. Union is preferred when only one member of declaring structure.
the union needs to be accessed at one time.
4. Members of the union can’t be initialized as Structure is preferred when we there is a need

they share memory. Value of the latter to access all the members of structure at same

member will replace or overwrite the value of time.

former member
Members of the structure can be initialized as
they do not share memory and all the members
can be accessed at same time

You might also like