unit - iv C prog ppt

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 49

Structures

Structure Definition:
 Structure is a user defined data type. It is a

collection of different data type, to create a new


data type.
 For example, You can define your custom type for

storing student record containing name, age and


mobile. Creating structure type will allow you to
handle all properties of student with single
variable, instead of handling it separately.
 In short it is a data structure in which we can

collect different types of data into one entity.


Declaration of Structure
To declare or define a structure, we use struct keyword. It is
a reserved word in the C . We must use it for structure definition or
its variable declaration.The following syntax is for defining a
structure with its members.
 Syntax:

struct tagname // where struct is the keyword and tagname is the


structure name
{
type member name 1;
type member name 2;
type member name 3; //Declaration of Structure members
" "
" "
type member name n;
};
Declaration of structure variables:
 A data type is useless without variables. A data type

defines various properties about data stored in memory.


To use any type we must declare its variable. Hence, let
us learn how to create our custom structure type objects
also known as structure variable.

In C programming, there are two ways to declare a


structure variable:
Along with structure definition
After structure definition
Declaration along with the structure definition
 In this we can declare a structure variable along with

structure before terminating the structure definition.


Syntax:
struct structure_name
{
member1_declaration;
member2_declaration;
... ...
memberN_declaration;
}
structure_variable;
 So, if it is needed to declare student type
variable along with student structure definition
we can use this approach.
 Example:

struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long mobile; // Student mobile
number
}s1;
Declaration after structure definition
 The other way to declare structure variable

anywhere in program based on the


structure scope. If structure is defined in
global scope, we can declare its variable in
main() function, any other functions and
in the global section too.
 Syntax:
struct tagname variablename
where struct is the Keyword ,tagname is the structure
name and variablename is the structure variable
 Example:

struct student
{
char name[40]; //Student name
int age; // Student name int age;
unsigned long mobile; // Student mobile number
};
struct student s1; // Declare student variable s1
Accessing Structure fields:
 Since structure is a complex data type, we cannot

assign any value directly to it using assignment


operator. We must assign data to individual structure
members separately and C supports two operators to
access structure members, using a structure variable.
Dot/period operator .
Arrow operator ->
 Dot/period operator also known as

member access operator. We use dot


operator to access members of simple
structure variable.
To refer to a member, use the syntax ,
 structurevariablename.member name;
 ‘.’ is the dot operator used between the

structure variable and structure member.


 Example:
 student1.age = 26;

Since structure is a user defined type and


we can have pointers to any type. Hence,
we may also create pointers to
structure.and in that case We use arrow
operator -> to access structure member
from pointer to structure.
Structure Initialization
 Structure can be initialized by two methods

Method 1
 struct studentDetails std={"Mike",21,{15,10,1990}};OR

 struct studentDetails std={"Mike",21,15,10,1990};

 Here, "Mike" and 21 will be stored in the members name and

age of structure studentDetails, while values 15, 10 and 1990


will be stored in the dd, mm, and yy of structure dateOfBirth.
 Here, is the example by initializing the values using this

method (Only main() function is here, place given structure


definitions before the main() )
int main()
{
struct studentDetails
std={"Mike",21,15,10,1990};
printf("Name: %s, Age: d\
n",std.name,std.age);
printf("DOB: %d /%d /%d \
n",std.dob.dd,std.dob.mm,std.dob.yy);
return 0; }
Method 2
 struct dateOfBirth dob={15,10,1990};
 struct studentDetails std={"Mike",21,dob};
 Initialize the first structure first, then use

structure variable in the initialization


statement of main (parent) structure.
int main()
{
struct dateOfBirth dob={15,10,1990};
struct studentDetails std={"Mike",21,dob};
printf("Name: %s, Age: %d \
n",std.name,std.age);
printf("DOB: %d/%d/%d
\n",std.dob.dd,std.dob.mm,std.dob.yy);
return 0;
}
Array of Structure

 Array of structure can be created as with


any other data types.
 In this we define the structure and then

create an array of structure type.


With structures, you can store mixed record
types, and with an array supporting this,
you can have a list of mixed record types
 An object of structure represents a single
record in memory, if we want more than
one record of structure type, we have to
create an array of structure or object. As we
know, an array is a collection of similar
type, therefore an array can be of structure
type.
Suppose you want to create a variable of
the mentioned structure in C,
 struct Employee myEmp;
 Example:
#include <stdio.h>
void main ()
{
struct student
{
int id;
char name [10];
};
struct student std [20];
int i,n;
printf (“enter the number of students \n”);
scanf (“%d”, &n);
for (i=0;i<n;i++)
{
printf (“enter the details of student [%d]\n”, (i+1));
printf (“enter id\n”);
scanf (“%d”, &std [i].id);
printf (“enter name\n”);
scanf (“%s”, std [i].name);
}
for (i=0: i<n; i++)
{
printf (“student [%d] details\n”, (i+1));
printf (“Id = %d\t name=%s\n”,std[i].id,std[i].name];
}
}
 In the above program an array std is created of type
student i.e) struct student std [20]. Then using the loops
each element of the array std is accessed.
Arrays can be initialized as,
struct student std [ ] = {001, “AAA”, 002, “BBB”, 003,
“CCC”,004 “DDD”};
 In the above case since the size of the array is not

specified, it takes the size depending on the initial


values, in this case array size is initialized to 4.
Array of Structure

 Array of structure can be created as with


any other data types.
 In this we define the structure and then

create an array of structure type.

 With structures, you can store mixed record


types, and with an array supporting this,
you can have a list of mixed record types
 An object of structure represents a single
record in memory, if we want more than
one record of structure type, we have to
create an array of structure or object. As we
know, an array is a collection of similar
type, therefore an array can be of structure
type.
Suppose you want to create a variable of
the mentioned structure in C,
 struct Employee myEmp;
 Example:
#include <stdio.h>
void main ()
{
struct student
{
int id;
char name [10];
};
struct student std [20];
int i,n;
printf (“enter the number of students \n”);
scanf (“%d”, &n);
for (i=0;i<n;i++)
{
printf (“enter the details of student [%d]\n”, (i+1));
printf (“enter id\n”);
scanf (“%d”, &std [i].id);
printf (“enter name\n”);
scanf (“%s”, std [i].name);
}
for (i=0: i<n; i++)
{
printf (“student [%d] details\n”, (i+1));
printf (“Id = %d\t name=%s\
n”,std[i].id,std[i].name];
}
}
Array within Structure

 A structure can also contain array members


just like normal members such int, float
We can declare an array if we need to store
multiple values inside structure.
 Structure may include as many array

members as we require.
 Syntax for declaring array within structure is

not different than the conventional syntax.


 The only difference is that it is declared

inside the structure.


 Syntax for array within structure
struct struct-name
{ datatype var1; // normal variable
datatype array [size]; // array variable
----------
----------
datatype varN; };
struct struct-name obj;
 Purpose of Array within Structure
When you find yourself to store a string
value, then you have to go for array within
structure. because your name comes under
character data type alone, thus array is
capable of storing data of same data type.
Nested Structures

 Nested structure in C is nothing but structure


within structure. One structure can be
declared inside other structure as we declare
structure members inside a structure. A
structure may be defined as a member of
another structure. The declaration of the
embedded structure must occur before the
declaration of the outer structure. This is also
said to structure within structure.
 A structure inside another structure is called
nested structure.
 The inner most member in a nested
structure can be accessed by changing all
the concerned structure variables (from
outer most to inner most) with the member
using dot operator.
The variables inside a structure can be
anything like normal or pointer or anything
and can be placed anywhere within the
structure.
#include <stdio.h>

void main ()
{
struct date
{
int dd;
int mm;
int yy;
};
struct student
{
char name [20];
struct date dob;
}
struct student s1;
printf (“enter student name\n”);
scanf (“%s”, s1.name);
printf (“enter date of birth\n”);
scanf (“%d %d %d”, &s1.dob.dd,
&s1.dob.mm, &s1.dob.yy);
printf(“name=%s\t DOB=%d/%d/%d\
n”,s1.name,s1.dob.dd,
s1.dob.mm,s1.dob.yy);
}
Comparison of structure variables

 Comparison
In C programming language, a structure is a
collection of different datatype variables,
which are grouped together under a single
name.
Declaration and initialization of structures
 The general form of a structure declaration is as

follows −
datatype member1;
struct tagname{
datatype member2;
datatype member n;
};
Here,
 struct is a keyword.

 tagname specifies the name of structure.

 member1, member2 specifies the data items that

make up structure.
 For example,
struct book
{ int pages;
char author [30];
float price;
};
Structure variables
 There are three methods of declaring

structure variables, which are as follows −


First method
struct book
{ int pages;
char author[30];
float price;
}b;
 Second method
struct{
int pages;
char author[30];
float price;
}b;
 Third method
struct book{
int pages;
char author[30];
float price; };
struct book b;
Initialization and accessing of
structures
The link between a member and a structure
variable is established by using a member
operator (or) a dot operator.
 Initialization can be done in the following
methods −
 First method

struct book{
int pages;
char author[30];
float price; }
b = {100, “balu”, 325.75};
 Second method
struct book{
int pages;
char author[30];
float price; };
struct book
b = {100, “balu”, 325.75};
Third method by using a member
operator
struct book{
int pages;
char author[30];
float price; } ;
struct book b;
b. pages = 100;
strcpy (b.author, “balu”);
b.price = 325.75;
Unions

 Unions are conceptually similar to


structures. The syntax to declare/define a
union is also similar to that of a structure.
 The only differences is in terms of storage.
 In structure each member has its own

storage location, whereas all members of


union uses a single shared memory location
which is equal to the size of its largest data
member.
This implies that although a union may
contain many members of different types, it
cannot handle all the members at the
same time. A union is declared using
the union keyword.
 Union like structure contains members of
different data types. Only difference is in
allocation of memory. In case of structure
memory is allocated for all its members, but
in case of union a piece of storage is
allotted which is large enough to hold the
largest variable type in the union.
i.e.) struct student
 Example:

# include <stdio.h>
void main ()
{
union student
{
int id;
char name [20];
};
union student s1;
printf (“enter student id\n”);
scanf (“ID=%d”, &s1.id);
printf(“Student id:%d\n”,s1.id);
printf (“enter student name\n”);
scanf (“Name=%s”, s1.name);
printf (“Student name = %s\n”,s1.name);
}
Output:
enter student id 10

ID=10
enter student name Liah
Name=Liah

10

You might also like