Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

STRUCTURE

USER-DEFINED DATATYPES

• structure (keyword - struct)

• union (keyword - union)

• enumeration : (keyword - enum)


STRUCTURES

Idea :
• Real world entities are defined by attributes or fields.

•For Ex: Student, Employee, Points in Geometry

Hence we want to store a collection of related data


items
Syntax :-
struct <structName>
{
<type> <memberName1>;
<type> <memberName2>;
<type> <memberName3>;
......
};
DECLARING STRUCTURES

struct student Every struct needs a


{ name

char name[20];
int idno;
float cg; Parts of the struct are
}; known as members

This declares a type of


Do not forget
structure, but it does not
the semicolon
here!
create a variable
struct student
{
char name[20];
int idno;
float cg;
};
This merely declares a new data type called student.
You can then use it to create variables of type student.

Important:- student is not a variable. There is


no memory allocated for it. It is merely a type
(like int, float, etc).
DECLARING STRUCTURES

This informs the compiler that


struct student the user has defined his own
data type called struct student
{ (No Memory is allocated at
char name[20]; this point since there is no
variable created yet)
int idno;
float cg;
};
s1.name
struct student s1; s1.id
s1.cg

This statement declares that s1 is a variable which is a


structure variable. After successful execution of this
statement some bytes are allocated to s1.
DECLARING STRUCTURES

struct student
{
char name[20];
int idno;
float cg;
}s1;
SOME MORE EXAMPLE

struct LibraryBook
{
int isbn, copies, pYear;
char bookName[64], authorName[64],publisherName[32];
};
SOME MORE EXAMPLE

• Complex Number : It has real and imaginary part.

• Point in 2D plane : It has x-coordinate and y-coordinate.


ACCESSING STRUCTURE MEMBERS
(.) dot Operator
struct student
{
char name[20]; 1000
int idno; 20 bytes s1.name
float cg; 1020
1021
}; s1.id
1022
struct student s1; 1023
s1.cg

s1.name 1027

s1.idno This gives access to value of s1’s


name.
s1.cg
ACCESSING STRUCTURES (CONT)

• A member of a structure is just like any other variable


– If it's a string, it's just an ordinary string
– If it's an int, it's just an ordinary int
• EXCEPT that you access them using the name of the struct
variable, AND the name of the member:
– s1.idno = 5599;
– strcpy(s1.name,“ABC");
– S1.cg = 9.0;
STRUCTURE VARIABLES DECLARATION

struct book
struct book struct
{
{ {
char name[20] ;
char name[20]; char name[20];
float price ;
float price ; float price ;
int pages ; Same as Same as
int pages ; int pages ;
};
} b1, b2, b3 ; } b1, b2, b3 ;
struct book b1, b2, b3 ;

recommended way
READ STUDENT DETAILS AND PRINT IT
Structure variables Initialization

If you miss this, compilation


error is generated
TYPEDEF

• typedef keyword is used to rename the existing data type i.e. giving alias name.
• A typedef declaration does not reserve storage.

struct student
{
char name[20];
int idno;
float cg;
};

typedef struct student ug;


ug s1;
TYPEDEF

typedef struct
{
char name[20];
int idno;
float cg;
}ug;

ug s1;
EX1 : COMPLEX NUMBER
ADDITION
EX2 : DISTANCE OF POINT FROM
ORIGIN
ADDITIONAL FEATURES OF
STRUCTURES
• You can use assignment operator
• Initialize
• Copy structures
• You can use address operator

But cannot use other operator like

• comparison operator
ADDITIONAL FEATURES OF
STRUCTURES
• The values of a structure variable can be assigned to
another structure variable of the same type using the
assignment operator.
struct student s1 == s2
{ s1 != s2
char name[20];
int idno;
float cg;
}s1={“john”,1223,9.8}; Comparisons not
possible
struct student s2;
s2 = s1;
HOW TO CHECK FOR EQUALITY OF 2 STRUCTURE
VARIABLES/OBJECTS

If(s1.idno == s2.idno && s1.cg == s2.cg &&


strcmp(s1.name, s2.name) == 0)
printf(“Same Details…”);
else
printf(“Different Details…”);
STRUCTURES WITHIN STRUCTURES

• Structures can be nested


i.e. It is possible that member of a structure can be
some structure.

• It is quite often used and is useful


EXAMPLE 1
EXAMPLE 2
ARRAY OF STRUCTURES
struct student struct student
{ slist[100];
int idno;
Creates an array of
char name[20]; structures that can hold 100
students details
};

struct student s1;


struct student s2;
struct student s3;
struct student s4; Akward to write like
struct student s5; this if 100 students are
there in a course
ARRAY OF
structs
sList
id:123456789
0
name: "fred"

1 id:123456788

name: "ralph"

2 id: 123456787

name: "fong"

id: 123456786
3
name: "rachel"
sList
id:123456789 sList[0]gives you the whole
0 struct
name: "fred"

1 id:123456788

name: "ralph"

2 id: 123456787

name: "fong"

id: 123456786
3
name: "rachel"
ARRAYS OF STRUCTS
sList
id:123456789
0
name: "fred"

1 id:123456788

name: "ralph"

2 id: 123456787

name: "fong"

sList[3].name gives you the


id: 123456786
3 struct member
name: "rachel"
ARRAYS OF
STRUCTS
struct student sList[100];
int i;
for (i=0;i<100;i++)
{ name of
printf("enter name\n"); array

scanf("%s",sList[i].name);
printf("enter id\n");
scanf("%d",&sList[i].id);
printf("enter CG\n");
scanf("%f",&sList[i].cg);

}
HOW TO READ AND WRITE ARRAY OF
STRUCTURES?
ARRAY OF STRUCTURES :
INITIALIZATION student[0].subj1=?;
struct marks student[0].subj2 =?;
{ student[2].subj3 =?;
int subj1;
45
int subj2;
68
int subj3;
81
}; 75
int main() 53
{ 69
struct marks student[3]= {{45,68,81}, 57
{75,53,69}, 36
{57,36,71} 71
};

}
PREFERENCE OF . OPERATOR

dot/period (.) Operator has higher precedence over ++, --,


- , Arithmetic operators …
-> OPERATOR
EXPOSURE ONLY
• If you have a pointer to a structure then use
( →) Operator to access the member variable
ptrVar->member
• Ex :
-> OPERATOR
EXPOSURE ONLY

• Dynamic Allocation :
PARAMETER PASSING IN A
FUNCTION

• Structure variables can be passed as parameters like


any other variables. Only the values will be copied
during function invocation.
DISADVANTAGE OF PASSING BY
CALL BY VALUE

• Each members of structure will be copied in the calling


function. Hence if a structure is having say 100 members
each will be copied which is in-efficient way of passing a
parameter
PASSING STRUCTURE PARAMETER
AS POINTER IN FUNCTIONS

• Passing address is best way of passing information


about structure. Since you need to pass just the address
of the variable.
RETURNING STRUCTURES

• It is also possible to return structure values from a


function. The return data type of the function should
be as same as the data type of the structure itself.
RETURNING STRUCTURES
complex *add(complex *c1, complex *c2){
complex *c3 = malloc(sizeof(complex));
c3->real = c1->real+c2->real;
c3->imag=c1->imag+c2->imag;
return c3;
}

int main()
{
complex c1 = {7,8};
complex c2 = {4,5}; printf("\nhi\n");
complex *c3 = add(&c1, &c2);
printf("%d %d", c3->real,c3->imag);
return 0;
}
SORT A LIST OF STUDENT ON BASIS
OF CG

• You compare 2 objects on cg.

You might also like