0% found this document useful (0 votes)
16 views6 pages

Adobe Scan May 13, 2024

Book

Uploaded by

Shruthi S
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)
16 views6 pages

Adobe Scan May 13, 2024

Book

Uploaded by

Shruthi S
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/ 6

5.

314
clements Write sumdigit(int
{ int x) *
ptr
main()
void Function
getch(): printf("n printf("n
for ptr } scanf(%d",
printf("n &A|0clrscr();
printf("n
for &size); }:ptr = *ptr;int int return(s); x%10;d=
st=d+sumigit(x/10); if(x--0)s-0,d;int
size, a return
(i (i in C
size
&A[size1]scanf("%od,
: - ptrt+;ptr); 0;i<
i, program
reverse (0); sum
Element Elements Enter Enter A[30]; of
1;i>s - size; digit
%d the order.using
%od of it+)
integers size define
is i) array0; pointers
%d : of as
in intoarray:");
", reverSe to
Programming in
recursive C
it1, aray:", read
*ptr): order
in */
size); an
are:"):
AprillMay
2015)(AU array
Of
integers

and
print
ie

DEFININGA
STRUCTURE
A10.1.1 There e
nglSructure itemsINTRODUCTION
structure 3. 2. 1. autelotBookExamples: siname. . cture useto An10.1
araray
are The
Acccssing Defining Addressbo is is
Declaring three is collection
individual
may a
convenient a data a
Student collection
parts
be structure
of
structure
objects structure a STRUCTURES UNION
AND
structure
struct
name defined of structure different
one structures: STRUCTURESNTO
of of way
or as, variables type. different whose Structures
Unionand
datatype datatype
memberl;
datatype
moremember n;member2;
structure :name, elements for data
handlng elements
structure author,name, type
type. (i.e. types
objects). are items,
door title, roll referred a are
of
ariables]; data different it all
number.,
no,price, is of
street, items as not the
type
members. possible
year.marks. same
city. that of
are logically todata
stored use type. Chapter10
related an A If
under arTray. we
need 315
data
a
316
Programming in C Structures and Union 317

Where struct is a keyword; structure name is a name that identifies


declaratisotnsruc.tureThes,.
Syntax:
memberl, member2...... member n are individual member
structstruct name
members can be ordinary variables, pointers, arrays or other structures
It is used to store different types of data item under asingle name. data type var name;
data type var name;
Example:
struct library_ b o o k s sO 0B
{
char title[20]: Example:
charauthori15: otssods
int pages; struct library books
float price;
}b1; char title{20];
nobui2 char author[15];
The keyword struct declares a structure to hold the details of four data fields title
author, pages and price. Each member may belong to a different type of data. The int pages;
composition of this account is illustrated schematically in figure. float price;

Library books Structure struct library books bookl, book2, book3;


title 51)2 Memberie ganslbod The statement,
book3;
author struct library books bookl, book2,
Member
variables of type struct library books. The
page declares book1, book2 and book3are variables.
Member the structure
Compiler reserves the memory space for
price 10.1.3 STRUCTURE INITIALIZATION
Member
10.1.2 DECLARING STRUCTURE VARIABLES types are initialized.
in the same way as other data
A Structure can be initialized to the members of the
assigning some constants
Astructure variable declaration is similar to Initializing a structure means
the declaration of variable of aany other
data types. It includes the following statements. structure.
1. The keyword struct
2. The structure tag namne
3. List of variable names separated by commas
4. A terminating semicolon.
from Where a accessed
writing, byThe 10.1.4 Example: Syntax: 318
member
the members 285}:struct 250}:struct
variable ACCESSING }: library
books
struct struct structure
member 1,constant2.,tnatsnoc{ble={structure_variastruct
variablel;
datatype
variable2;
datatype
within library_books
of library_ pages;inttitle(20):
price;
float
author[15]: char char structure_name
refers structure_name
name. the a
structure Or
_variable={constant1,
constant2...};
structure. tostructure STRUCTURE books
It the
has book1={"computer
name book2={"workshop
are
the variable
The processed
highest of Programming In
C
period the name MEMBERS
structure,
cedence individually.
e
operalor member
practicel",
and practice",
(")
group. member variable
separates A
structure Dr.Suresh
name
L3AUTDUHTLID "Dr.
refers
the
Saravanan",
to member
Variable the Kumar",
name can
name 400, 346,
v k

Nagercoil
Entered Nager
is: Carmel 345 Output:
Sugitha maívoind 0
Sugitha,Address addrstrucetss
address
person;
struct door_number;
street[30]:
city[30]:
char charname[25];
ínt char
getch):
person.street,
person.city): person.street,
person.
printf("\nEntered
Addris:e\nssc);ity): printf("clrscr):
345, 1
Carmel Enter
s,%d,%s,%s\n"spcearnsfo(n"%.nas%md%se,per%sosn",.pdeorsro_n.nnuammbee,&r, person.d_oornumber,
HincluHdein<cloundieo<.hst>dio.h>IlPlrougstramratio101n
Nager, the
Name,door of Unionand
accessingStructures
Nagercoil.
number, structure
members

Street
and
City
name:\n"):

319
Syntax:members using complex STRUCTURES
used structure NESTED10.2 320
Program A
structure
#include<stdio.h>CLRE
dot createto
struct
_data
{stud struct
date ! I of data is
10.2: structdeclarations; struct
declarations;
datatype
struct
the ()
operator,complex
innermost
and called ascan
name[20];
struct
dob; char
date date,month,year;int structure structure
Ilustration structure can placcdbe
helpnested
as inimprove
name2 namel types.
datThea
namel
of conjunction
well structure. within
nested variable as Programming in
C
the
program's aanother
structure outermost elements of Nested
name1; with
the structure. A
rcadability
structurestr
structures
structures.us
nested

39916.02100 variables
accessed
structuresare astructure
nd are
useful
maintainability.
oek
to within
for
accesSS organizng
another
the

10.3 Date of
Syntax: placing self ABirth:6-3-82
symbol When
referenced Here structure Suthan
Name:
Output:
Program10.3
main()
void
#include<stdio.h> referentialPOINTERS
structure
pointers an main()
{void
->
struct
is by asteriskcontaining
called using declarations; s={("Suthan"{06,03,82}}:
printf("\n stud_data
struct
clrscr();
Illustration are name
datatype struct
nametag structure. getch):
s.dob.month,
s.dob.year);
%s"s.name);
printf("\n
Name:
asthe (*) AND
pointing is
structuresymbol in a
a member
frontSTRUCTURES
A
of pointer, \nDate
structure pointer -> to pointer
of Structures
Unionand
a the that
(Hyphen ponting
structure, of
structure
variable is Birth:%d-%d-%d",
using symbol. pointer
a
to
pointers followed then the pointer for
structure the theto
the variable.
by structure same
structure
greater s.dob.date,
specified structure
can
than
elements be
symbol). in type
declared
tag is
canname. called 321
This be by
Programming in C Structures and Union 323
322

int rollno; Erample:


char namel30];: structmarks
char branch[4};
int marks; ínt subject1;
ínt subject2;
}'stud;
int subject3;
clrscr):
príntf(" nEnter Rollno :;
scanf("%d", &stud->rollno); main()
printf("\n Enter Name :); struct marks student[3]-{(95,92,89}(65,63,70},(87,76,61};,
scanf("%s"stud->name);
printf("\n Enter Branch:");
scanf("%s",stud->branch); elements student(0, student[1] and
Thís declares the student as an array of three
printf("\n Enter Marks:"; members.
student([2] and initializes their
scanf("%d" &stud->marks);UpUAT20O
structures
printf("\n RollNumber:%d"stud->rollno); Program 10.4 1llustration of arrays of
printf("\n Name:%s";stud->name);:bEguanto #include<stdio.h>
printf("\n Branch:%s" stud->branch);ot #include<conío.h>
printf("|n Marks:%d",stud->marks); struct marks
getch():
int subl;
Output: int sub2;
Enter Rollno :1000 int sub3;
Enter Name :Ebisha C int total;
Enter Branch:CSE
Enter Marks:90 void main0
{
Roll Number: 69,0},
Name:Ebisha
1000ecllo t)odebit unisVo os
int i;
struct marks student[3|
={(45, 67, 81, 0},{75, 53,
Branch:CSE {57, 36, 71, 0}:
Marks:90 struct marks total;
clrscr();
10.4 ARRAY OF for(i=0; i<=2; i++)
STRUCTURES
If we want to handle more student[i].sub2+ student[i].sub3;
records within one student[il. total =student[i].subl+
cases it is structure, necd not specify
number of structure variable. In such we the
elements are of structure type. Such an array is possible to create an array whose printf("TTAL MARKS\n\n"):
known as an array of structures.
Programnming in C Structures and Union 325

forti=0; i<=2; it+)


Program
10.5: Passing a copy of entire structure to afunction
printl|'student(%d}: 9%d\n", it1, student[|].total):
getch): #include<stdio.h>
#include<conio.h>
Output: structstd

TOTAL MARKS
int n o ; o b
student[ 1): 193 bAloatavg:
student|2]: 197
student[3]: 164
void fun(struct std p);
10.5 STRUCTURES AND FUNCTIONS void main(0
n8 s nobule oi
In C there are three methods by which the struct std a;
from one function to another function. They values of a structure can be transfermed t clrscr):
are,
1. Pass each member of the a.no=1;d
2. Pass a copy of the entire
structure as an actual argument of the function. a.avg=90.6;
structure to the called
3. Pass the address
location of the structure to the function.
called function.
fun(a);
getch);
The first method raises a few
when the structure size is large. practical issues like unmanageable and insufficient
void fun(struct std p)
In the second {
method, changes are made only the printf("Number is:%d\n"p.no):
modifications within the function are not reflected in the
copy of the structure. So
printf("Average is: %f\n".p.avg):
Syntax: original structure.
function name(structure_variable name); Output:
datatype function
name(struct_typest name) Number is
Average is
:1
:90.599998

10.6 SELF-REFERENTIAL STRUCTURES


reference to data of its
return(expression); structures that contain a
Selt-referential struetures are those self-referential structure contains a
pointer
type, .e., in addition to other data, a example, consider the
Same structure. For
is of the same type as that of the
The third methoduses the to adata that
pointers concept. Thi8 method is more structure node given as follows:
effcient.

You might also like