SlideShare a Scribd company logo
1 of 50
Structure in C
Structure
 A structure is a collection of variables of
different data types under a single name.
 The variables are called members of the
structure.
 The structure is also called a user-defined
data type.
Defining a Structure
Syntax:
struct structure_name
{
data_type member1;
data_type member2;
………………………………;
data_type memberN;
};
Example
struct Books
{
char title[50];
char author[50];
char publisher[50];
int book_id;
};
Structures are used to represent a record.
Suppose you want to keep track of your books in a library. You might want to track the
following attributes about each book −
Title
Author
Publisher
Book ID
Cont..
Multiple variables of struct student type can be declared as:
struct Books b1, b2, b3;
Each variable of structure has its own copy of member
variables.
The member variables are accessed using the dot (.)
operator or member operator.
For example:
b1.title is member variable name of b1 structure variable
while b3.book_id is member variable book_id of b3
structure variable.
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int citNo;
float salary;
} person;
int main() {
// create Person variable
person p1;
 // assign value to name of p1
 strcpy(p1.name, "George Orwell");
 // assign values to other p1 variables
 p1.citNo = 1984;
 p1. salary = 2500;
 // print struct variables
 printf("Name: %sn", p1.name);
 printf("Citizenship No.: %dn", p1.citNo);
 printf("Salary: %.2f", p1.salary);
 return 0;
 }
 Example:
struct student
{
char name[20];
int ID;
float CSE_marks;
char gender;
long int phone_no;
};
void main()
{
struct student st1={“Rajkumar",1577,17.5,'M',9940585840};
struct student st2={"Oshin",4288,15,'F',9119845321};
printf ("Name: %sn ID: %dn CSE Marks: %.1f nGender: %c
nPhn: %d n",st1.name,st1.ID,st1.CSE_marks,st1.gender,st1.phone_no);
printf ("Name: %sn ID: %d nCSE Marks: %.1f nGender: %cn Phn: %d
n",st2.name,st2.ID,st2.CSE_marks,st2.gender,st2.phone_no);
}
Output:
Name: Rajkumar
ID: 1577
CSE Marks: 17.5
Gender: M
Phone: 9940585840
Name: Oshin
ID: 4288
CSE Marks: 15.0
Gender: F
Phone: 9119845321
“Structure within another Structure
(Nested Structure)”
 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.
 The structure variables can be a normal structure variable or a pointer
variable to access the data.
 Nested Structures are allowed in C Programming Language.
 We can write one Structure inside another structure as member of another
structure.
Example:
struct dob
{
int dd;
int mm;
int yyyy;
};
struct student1
{
int vtuno;
char name[30];
struct dob d;
char dept;
}
int main()
{
struct dob d={6,12,2020};
struct student1 s3={1212,"Rajkumar",d,"CSE"};
printf("VTUNO:%dnName:%snDOB:%d/%d/%dnDepartment:%s",s3.vtuno,s3.name,s3.d.dd,s3.d.mm,s3.d.y
yyy,s3.dept);
}
Output:
VTUNO:1212
Name:Rajkumar
DOB:6:12:2020
Department:CSE
--------------------------------
Process exited after 0.1409 seconds with return value 0
Press any key to continue . . .
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int dd;
int mm;
int yyyy;
} doj;
} ;
int main()
{
struct Employee e;
scanf("%s",e.name);
scanf("%d",&e.ssn);
scanf("%f",&e.salary);
scanf("%d",&e.doj.date);
scanf("%d",&e.doj.month);
scanf("%d",&e.doj.year);
printf("Name:%snSSN:%dnSalary:%.1fnDOJ:%d:%d:%d",e.name,e.ssn,e.salar
y,e.doj.date,e.doj.month,e.doj.year);
}
Input:
Rajkumar
1234
50000
29
6
1975
Output
Name:Rajkumar
SSN:1234
Salary:50000.0
DOJ:29:6:1975
--------------------------------
Array of Structures
 An array is a collection of data items of the same type.
 Each element of the array can be int, char, float, double,
or even a structure.
 We have seen that a structure allows elements of
different data types to be grouped together under a single
name.
 This structure can then be thought of as a new data type
in itself.
 So, an array can comprise elements of this new data type.
 An array of structures finds its applications in grouping the
records together and provides for fast accessing.
struct student
{
int regno;
char
name[10],grade;
int m1,m2,m3;
float avg,tot;
};
void main()
{
struct student s[10];
int i,n;
printf("nEnter the no.of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("nEnter the student regno,name,m1,m2,m3:");
scanf("%d%s%d%d%d",&s[i].regno,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
if(s[i].m1<35||s[i].m2<35||s[i].m3<35)
s[i].grade='f';
else
{
if(s[i].avg>=75)
s[i].grade=‘A';
else if(s[i].avg>=60)
s[i].grade=‘B';
else if(s[i].avg>=50)
s[i].grade=‘C';
else if(s[i].avg>=35)
s[i].grade=‘D';
}
}
printf("nSTUDENT MARK LISTn");
printf("nREGNOtNAMEtTOTALtAvgtGRADE");
for(i=0;i<n;i++)
printf("n%dt%st%ft%ft%c",s[i].regno,s[i].name,s[i].tot,s[i].avg,s[i].grade);
getch();
}
Enter the no.of students:2
Enter the student regno,name,m1,m2,m3:101
John
89
98
78
Enter the student regno,name,m1,m2,m3:102
Chini
59
68
76
STUDENT MARK LIST
REGNO NAME TOTAL Avg GRADE
101 John 265.00 88.33 A
102 Chini 203.00 67.67 B
Union
 An Union is a collection of different data items,
that are stored under a common name. Here
same memory is shared by its members.
 Syntax:
union union _name
{
union element1;
union element2;
…………………
};
14
N.Rajkumar
 Example:
union result
{
int mark;
float avg;
char grade;
};
15
N.Rajkumar
// C Program to demonstrate how to
use union
#include <stdio.h>
// union template or declaration
union un {
int member1;
char member2;
float member3;
};
// driver code
int main()
{
// defining a union variable
union un var1;
// initializing the union member
var1.member1 = 15;
printf("The value stored in member1 =
%d",
var1.member1);
return 0;
}
OUTPUT
The value stored in member1 = 15
Example
#include<stdio.h>
#include<conio.h>
union stud
{
int a;
char b[2];
};
void main()
{
union stud c;
17
N.Rajkumar
c.a=256;
printf("nc.a value is%d",c.a);
}
Output:
c.a value is256
18
N.Rajkumar
Structure and Union Difference
STRUCTURE UNION
The struct keyword is used to define a
structure.
The union keyword is used to define
union.
When the variables are declared in a
structure, the compiler allocates memory
to each variables member. The size of a
structure is equal or greater to the sum of
the sizes of each data member.
When the variable is declared in the
union, the compiler allocates
memory to the largest size variable
member. The size of a union is equal
to the size of its largest data
member size.
Each variable member occupied a
unique memory space.
Variables members share the
memory space of the largest size
variable.
Changing the value of a member will not
affect other variables members.
Changing the value of one member
will also affect other variables
members.
Each variable member will be assessed
at a time.
Only one variable member will be
assessed at a time.
We can initialize multiple variables of a
structure at a time.
In union, only the first data member can
be initialized.
Structure and Union Difference
STRUCTURE UNION
All variable members store some value at
any point in the program.
Exactly only one data member
stores a value at any particular
instance in the program.
The structure allows initializing multiple
variable members at once.
Union allows initializing only one
variable member at once.
It is used to store different data type
values.
It is used for storing one at a time
from different data type values.
It allows accessing and retrieving any
data member at a time.
It allows accessing and retrieving
any one data member at a time.
Size of Union
• The size of the union will always be equal to the size of the largest member of the
array.
• All the less-sized elements can store the data in the same space without any
overflow.
Union1&Union2
// C Program to find the size of the union
#include <stdio.h>
// declaring multiple unions
union test1 {
int x;
int y;
} Test1;
union test2 {
int x;
char y;
} Test2;
union test3 {
int arr[10];
char y;
} Test3;
// driver code
int main()
{
// finding size using sizeof() operator
int size1 = sizeof(Test1);
int size2 = sizeof(Test2);
int size3 = sizeof(Test3);
printf("Sizeof test1: %dn", size1);
printf("Sizeof test2: %dn", size2);
printf("Sizeof test3: %d", size3);
return 0;
}
OUTPUT :
Sizeof test1: 4
Sizeof test2: 4
Sizeof test3: 40
POINTERS
 Pointer is a variable that contains the memory address of another
variable.
 Every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand
(&) operator.
 Advantages:
 It supports dynamic memory allocation and reallocation of
memory segments.
 Variables can be swapped without physically moving them
 It reduces the length and complexity of the program
N.Rajkumar 24
a=10
Example
Example
#include<stdio.h>
void main()
{
int a=5;
printf("n The Address of a = %u",&a);
printf("n The Value of a = %d",a);
}
Output
The Address of a = 8714
The Value of a = 5
25
N.Rajkumar
Pointer Declaration
• Syntax
data-type* pointer-name;
data-type - Type of the data to
which the pointer points.
pointer-name - Name of the pointer
• Example: int *a;
26
N.Rajkumar
N.Rajkumar 27
int a=10;
int *ptr;
ptr=&a;
void main()
{
int a=10;
int *p;
p=&a; //p=a its not possible
printf(“The address of a%d=”,&a);
printf(“The address of p=%d”,&p);
printf(“The value of a =%d”,a);
printf(“Actual value of p=%d”,p);
printf(“The value of p=%d”,*p);
}
Types of Pointers in C
pointers to primitive data types, pointers to
user defined data types
 Integer Pointers----- int *ptr;
 Array Pointer------ char *ptr = &array_name;
 Structure Pointer---- struct struct_name *ptr;
 Function Pointers--- int (*ptr)(int, char);
 Double Pointers----- datatype ** pointer_name;
 *pointer_name; // get the address stored in the inner level pointer
 **pointer_name; // get the value pointed by inner level pointe
 NULL Pointer ---
data_type *pointer_name = NULL;
or
pointer_name = NULL
 Void Pointer
void * pointer_name;
 Constant Pointers
data_type * const pointer_name; (Address)
Pointer to Constant
const data_type * pointer_name; (value)
Other Types of Pointers in C:
 Far pointer: A far pointer is typically 32-bit that can access memory outside
the current segment.
 Dangling pointer: A pointer pointing to a memory location that has been
deleted (or freed) is called a dangling pointer.
 Huge pointer: A huge pointer is 32-bit long containing segment address and
offset address.
 Complex pointer: Pointers with multiple levels of indirection.
 Near pointer: Near pointer is used to store 16-bit addresses means within the
current segment on a 16-bit machine.
 Normalized pointer: It is a 32-bit pointer, which has as much of its value in
the segment register as possible.
 File Pointer: The pointer to a FILE data type is called a stream pointer or a
file pointer.
Size of Pointers in C
 The size of the pointers in C is equal for every pointer type.
 The size of the pointer does not depend on the type it is pointing to. It only
depends on the operating system and CPU architecture.
 The size of pointers in C is
 8 bytes for a 64-bit System
 4 bytes for a 32-bit System
C Program to find the size of different pointer types.
#include <stdio.h>
struct str {
};
// dummy function
void func(int a, int b){};
int main()
{
// dummy variables definitions
int a = 10;
char c = 'G';
struct str x;
// pointer definitions of different types
int* ptr_int = &a;
char* ptr_char = &c;
struct str* ptr_str = &x;
void (*ptr_func)(int, int) = &func;
void* ptr_vn = NULL;
// printing sizes
printf("Size of Integer Pointer t:t%d bytesn",
sizeof(ptr_int));
printf("Size of Character Pointert:t%d bytesn",
sizeof(ptr_char));
printf("Size of Structure Pointert:t%d bytesn",
sizeof(ptr_str));
printf("Size of Function Pointert:t%d bytesn",
sizeof(ptr_func));
printf("Size of NULL Void Pointert:t%d bytes",
sizeof(ptr_vn));
return 0;
} OUTPUT
Size of Integer Pointer : 8 bytes
Size of Character Pointer : 8 bytes
Size of Structure Pointer : 8 bytes
Size of Function Pointer : 8 bytes
Size of NULL Void Pointer : 8 bytes
POINTER ARITHMETIC
• Arithmetic operation supported pointers are
– Increment
– Decrement
– Adding integer value with pointer
– Subtracting integer value with pointer
• The following arithmetic operations are not
allowed with pointers
– Multiplication
– Division
– Add or subtract float/double with pointers…
N.Rajkumar 34
• Increment:
int x=10, *p;
p=&x;
p++;
• p++ will not add 1 to p, but add size of that
data type with p, which is pointed by the
pointer
• If p is 1000(Addr of x) after the statement
p=&x, then after p++, p will be 1004, because
the size of x is 4 bytes
N.Rajkumar 35
• Decrement:
int x, *p1,*p2,*p3;
p1=&x;
p2=p1--;
p3=p1--;
• p1-- will not decrement 1 to p1, but subtract
the size of that data type with p1, which is
pointed by the pointer
• If p1 is 1000(Addr of x) after the statement
p1=&x then:
• After the statement p2=p1--, p2=998
• After the statement p3=p1--, p3=996
N.Rajkumar 36
#include<stdio.h>
#include<conio.h>
void main()
{
int x=100;
int *ptr;
ptr=&x;
clrscr();
printf(“Address of x before increment = %u”,ptr);
ptr++;
printf (“Address of x after increment = %u”,ptr);
printf (“Address of x before decrement = %u”,ptr);
ptr--;
printf(“Address of x after decrement = %u”,ptr);
getch();
} N.Rajkumar 37
//Program to display the memory address
of a variable using pointers, before and
after increment and decrement
• Adding integer value with pointer:
int *p,x;
x=10;
p=&x;
p=p+7;
• If p is 1000(Addr of x), then after the statement p=p+7, p will
be 1014.
• Subtracting integer value with pointer:
int *p,x;
x=10;
p=&x;
p=p-7;
• If p is 1000(Addr of x), then after the statement p=p-7, p will
be 986.
N.Rajkumar 38
#include<stdio.h>
#include<conio.h>
void main()
{
int x, *p1, *p2, *p3;
x=20;
p1=&x;
p2=p1+7;
p3=p1-7;
printf(“Value of p1=%d”, *p1);
printf(“nAddress of p1=%u”, p1);
printf(“nAddress of p2=% u”, p2);
printf(“nAddress of p3=% u”, p3);
getch();
} N.Rajkumar 39
//Program for adding and subtracting
integer value with pointer
Pointer to an Array
N.Rajkumar 40
#include<stdio.h>
#include<string.h>
int main()
{
int a[5]={1,2,3,4,5};
int* p,i;
char *p1;
char name[]="Rajkumar";
p=&a;
p1=&name;
for(i=0;i<5;i++,p++)
printf("%dn",*p);
for(i=0;i<strlen(name);i++,p1++)
printf("%c",*p1);
}
N.Rajkumar 41
#include<stdio.h>
int main()
{
int a[30];
int i,sum=0,*p,n;
printf("Enter the no of elements in array:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=&a;
for(i=0;i<n;i++,p++)
sum=sum+*p;
printf("sum of array elements =%d ",sum);
}
N.Rajkumar 42
//Find Minimum and Maximum
#include<stdio.h>
int main()
{
int a[]={31,21,13,44,51,16,17,28};
int *p,i=30,max,min;
p=a;
max=min=*p;
printf("p=%d",p);
for(i=0;i<8;i++,p++)
{
if(*p>max)
max=*p;
if(*p<min)
min=*p;
}
printf("max=%d min=%dn",&max,&min);
}
N.Rajkumar 43
//Find Minimum and Maximum
#include<stdio.h>
int main()
{
int a[]={31,21,13,44,51,16,17,28};
int *p,i=30,max,min;
p=a;
max=min=*p;
printf("p=%d",p);
for(i=0;i<8;i++,p++)
{
if(*p>max)
max=*p;
if(*p<min)
min=*p;
}
printf("max=%d min=%dn",&max,&min);
}
N.Rajkumar 44
//Example for Pointer to structure
#include<stdio.h>
struct student
{
int vtuno;
char name[30];
};
int main()
{
struct student s[10];
struct student *p;
int i;
printf("Enter the student Details:");
for(i=0;i<3;i++)
{
printf("Enter the vtuno:");
scanf("%d",&s[i].vtuno);
printf("Enter the student name:");
scanf("%s",s[i].name);
}
p=&s;
for(i=0;i<3;i++,p++)
{
printf("%dt%sn",p->vtuno,p->name);
}
}
Parameter Passing Methods
• Call by value
• Call by reference
45
N.Rajkumar
Call by value
• Actual argument passed to the formal argument.
• Any changes to the formal argument does not affect the actual
argument.
46
N.Rajkumar
Example
#include <stdio.h>
#include<conio.h>
int add(int,int);
int main()
{
int a,b,c;
clrscr();
printf("nEnter two number:");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("nSum is:%d",c);
}
int add(int x,int y)
{
int z;
z=x+y;
x=x+20;
return(z);
}
47
N.Rajkumar
Output
Enter two number:6
7
Sum is:13
48
N.Rajkumar
Call by reference
• Instead of passing value, the address of the argument will be
passed.
• Any changes to the formal argument will affect the actual
argument.
49
N.Rajkumar
Example
#include <stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int x,y;
printf("nEnter value of x:");
scanf("%d",&x);
printf("nEnter value of y:");
scanf("%d",&y);
Output
Enter value of x:5
Enter value of y:6
x=6,y=5
swap(&x,&y);
printf("nx=%d,y=%d",x,y);
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf("nx=%d,y=%d",*a,*b);
}
50
N.Rajkumar

More Related Content

Similar to data structure and c programing concepts

Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture topu93
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handlingRai University
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structureDeepak Singh
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Structures
StructuresStructures
Structuresselvapon
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING Gurwinderkaur45
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.pptshivani366010
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)mrecedu
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxGebruGetachew2
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxDeepasCSE
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RERajeshkumar Reddy
 

Similar to data structure and c programing concepts (20)

Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Structure In C
Structure In CStructure In C
Structure In C
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
Structures
StructuresStructures
Structures
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Structures
StructuresStructures
Structures
 
C structure and union
C structure and unionC structure and union
C structure and union
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 

Recently uploaded

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degreeyuu sss
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 

Recently uploaded (20)

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 

data structure and c programing concepts

  • 2. Structure  A structure is a collection of variables of different data types under a single name.  The variables are called members of the structure.  The structure is also called a user-defined data type.
  • 3. Defining a Structure Syntax: struct structure_name { data_type member1; data_type member2; ………………………………; data_type memberN; };
  • 4. Example struct Books { char title[50]; char author[50]; char publisher[50]; int book_id; }; Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − Title Author Publisher Book ID
  • 5. Cont.. Multiple variables of struct student type can be declared as: struct Books b1, b2, b3; Each variable of structure has its own copy of member variables. The member variables are accessed using the dot (.) operator or member operator. For example: b1.title is member variable name of b1 structure variable while b3.book_id is member variable book_id of b3 structure variable.
  • 6. #include <stdio.h> #include <string.h> struct Person { char name[50]; int citNo; float salary; } person; int main() { // create Person variable person p1;  // assign value to name of p1  strcpy(p1.name, "George Orwell");  // assign values to other p1 variables  p1.citNo = 1984;  p1. salary = 2500;  // print struct variables  printf("Name: %sn", p1.name);  printf("Citizenship No.: %dn", p1.citNo);  printf("Salary: %.2f", p1.salary);  return 0;  }
  • 7.  Example: struct student { char name[20]; int ID; float CSE_marks; char gender; long int phone_no; }; void main() { struct student st1={“Rajkumar",1577,17.5,'M',9940585840}; struct student st2={"Oshin",4288,15,'F',9119845321}; printf ("Name: %sn ID: %dn CSE Marks: %.1f nGender: %c nPhn: %d n",st1.name,st1.ID,st1.CSE_marks,st1.gender,st1.phone_no); printf ("Name: %sn ID: %d nCSE Marks: %.1f nGender: %cn Phn: %d n",st2.name,st2.ID,st2.CSE_marks,st2.gender,st2.phone_no); } Output: Name: Rajkumar ID: 1577 CSE Marks: 17.5 Gender: M Phone: 9940585840 Name: Oshin ID: 4288 CSE Marks: 15.0 Gender: F Phone: 9119845321
  • 8. “Structure within another Structure (Nested Structure)”  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.  The structure variables can be a normal structure variable or a pointer variable to access the data.  Nested Structures are allowed in C Programming Language.  We can write one Structure inside another structure as member of another structure.
  • 9. Example: struct dob { int dd; int mm; int yyyy; }; struct student1 { int vtuno; char name[30]; struct dob d; char dept; } int main() { struct dob d={6,12,2020}; struct student1 s3={1212,"Rajkumar",d,"CSE"}; printf("VTUNO:%dnName:%snDOB:%d/%d/%dnDepartment:%s",s3.vtuno,s3.name,s3.d.dd,s3.d.mm,s3.d.y yyy,s3.dept); } Output: VTUNO:1212 Name:Rajkumar DOB:6:12:2020 Department:CSE -------------------------------- Process exited after 0.1409 seconds with return value 0 Press any key to continue . . .
  • 10. struct Employee { char ename[20]; int ssn; float salary; struct date { int dd; int mm; int yyyy; } doj; } ; int main() { struct Employee e; scanf("%s",e.name); scanf("%d",&e.ssn); scanf("%f",&e.salary); scanf("%d",&e.doj.date); scanf("%d",&e.doj.month); scanf("%d",&e.doj.year); printf("Name:%snSSN:%dnSalary:%.1fnDOJ:%d:%d:%d",e.name,e.ssn,e.salar y,e.doj.date,e.doj.month,e.doj.year); } Input: Rajkumar 1234 50000 29 6 1975 Output Name:Rajkumar SSN:1234 Salary:50000.0 DOJ:29:6:1975 --------------------------------
  • 11. Array of Structures  An array is a collection of data items of the same type.  Each element of the array can be int, char, float, double, or even a structure.  We have seen that a structure allows elements of different data types to be grouped together under a single name.  This structure can then be thought of as a new data type in itself.  So, an array can comprise elements of this new data type.  An array of structures finds its applications in grouping the records together and provides for fast accessing.
  • 12. struct student { int regno; char name[10],grade; int m1,m2,m3; float avg,tot; }; void main() { struct student s[10]; int i,n; printf("nEnter the no.of students:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("nEnter the student regno,name,m1,m2,m3:"); scanf("%d%s%d%d%d",&s[i].regno,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3); s[i].tot=s[i].m1+s[i].m2+s[i].m3; s[i].avg=s[i].tot/3; if(s[i].m1<35||s[i].m2<35||s[i].m3<35) s[i].grade='f'; else { if(s[i].avg>=75) s[i].grade=‘A'; else if(s[i].avg>=60) s[i].grade=‘B'; else if(s[i].avg>=50) s[i].grade=‘C'; else if(s[i].avg>=35) s[i].grade=‘D'; } }
  • 13. printf("nSTUDENT MARK LISTn"); printf("nREGNOtNAMEtTOTALtAvgtGRADE"); for(i=0;i<n;i++) printf("n%dt%st%ft%ft%c",s[i].regno,s[i].name,s[i].tot,s[i].avg,s[i].grade); getch(); } Enter the no.of students:2 Enter the student regno,name,m1,m2,m3:101 John 89 98 78 Enter the student regno,name,m1,m2,m3:102 Chini 59 68 76 STUDENT MARK LIST REGNO NAME TOTAL Avg GRADE 101 John 265.00 88.33 A 102 Chini 203.00 67.67 B
  • 14. Union  An Union is a collection of different data items, that are stored under a common name. Here same memory is shared by its members.  Syntax: union union _name { union element1; union element2; ………………… }; 14 N.Rajkumar
  • 15.  Example: union result { int mark; float avg; char grade; }; 15 N.Rajkumar
  • 16. // C Program to demonstrate how to use union #include <stdio.h> // union template or declaration union un { int member1; char member2; float member3; }; // driver code int main() { // defining a union variable union un var1; // initializing the union member var1.member1 = 15; printf("The value stored in member1 = %d", var1.member1); return 0; } OUTPUT The value stored in member1 = 15
  • 17. Example #include<stdio.h> #include<conio.h> union stud { int a; char b[2]; }; void main() { union stud c; 17 N.Rajkumar
  • 19. Structure and Union Difference STRUCTURE UNION The struct keyword is used to define a structure. The union keyword is used to define union. When the variables are declared in a structure, the compiler allocates memory to each variables member. The size of a structure is equal or greater to the sum of the sizes of each data member. When the variable is declared in the union, the compiler allocates memory to the largest size variable member. The size of a union is equal to the size of its largest data member size. Each variable member occupied a unique memory space. Variables members share the memory space of the largest size variable. Changing the value of a member will not affect other variables members. Changing the value of one member will also affect other variables members. Each variable member will be assessed at a time. Only one variable member will be assessed at a time. We can initialize multiple variables of a structure at a time. In union, only the first data member can be initialized.
  • 20. Structure and Union Difference STRUCTURE UNION All variable members store some value at any point in the program. Exactly only one data member stores a value at any particular instance in the program. The structure allows initializing multiple variable members at once. Union allows initializing only one variable member at once. It is used to store different data type values. It is used for storing one at a time from different data type values. It allows accessing and retrieving any data member at a time. It allows accessing and retrieving any one data member at a time.
  • 21. Size of Union • The size of the union will always be equal to the size of the largest member of the array. • All the less-sized elements can store the data in the same space without any overflow. Union1&Union2
  • 22. // C Program to find the size of the union #include <stdio.h> // declaring multiple unions union test1 { int x; int y; } Test1; union test2 { int x; char y; } Test2; union test3 { int arr[10]; char y; } Test3; // driver code int main() { // finding size using sizeof() operator int size1 = sizeof(Test1); int size2 = sizeof(Test2); int size3 = sizeof(Test3); printf("Sizeof test1: %dn", size1); printf("Sizeof test2: %dn", size2); printf("Sizeof test3: %d", size3); return 0; } OUTPUT : Sizeof test1: 4 Sizeof test2: 4 Sizeof test3: 40
  • 23. POINTERS  Pointer is a variable that contains the memory address of another variable.  Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator.  Advantages:  It supports dynamic memory allocation and reallocation of memory segments.  Variables can be swapped without physically moving them  It reduces the length and complexity of the program
  • 25. Example #include<stdio.h> void main() { int a=5; printf("n The Address of a = %u",&a); printf("n The Value of a = %d",a); } Output The Address of a = 8714 The Value of a = 5 25 N.Rajkumar
  • 26. Pointer Declaration • Syntax data-type* pointer-name; data-type - Type of the data to which the pointer points. pointer-name - Name of the pointer • Example: int *a; 26 N.Rajkumar
  • 28. void main() { int a=10; int *p; p=&a; //p=a its not possible printf(“The address of a%d=”,&a); printf(“The address of p=%d”,&p); printf(“The value of a =%d”,a); printf(“Actual value of p=%d”,p); printf(“The value of p=%d”,*p); }
  • 29. Types of Pointers in C pointers to primitive data types, pointers to user defined data types  Integer Pointers----- int *ptr;  Array Pointer------ char *ptr = &array_name;  Structure Pointer---- struct struct_name *ptr;  Function Pointers--- int (*ptr)(int, char);  Double Pointers----- datatype ** pointer_name;  *pointer_name; // get the address stored in the inner level pointer  **pointer_name; // get the value pointed by inner level pointe
  • 30.  NULL Pointer --- data_type *pointer_name = NULL; or pointer_name = NULL  Void Pointer void * pointer_name;  Constant Pointers data_type * const pointer_name; (Address) Pointer to Constant const data_type * pointer_name; (value)
  • 31. Other Types of Pointers in C:  Far pointer: A far pointer is typically 32-bit that can access memory outside the current segment.  Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.  Huge pointer: A huge pointer is 32-bit long containing segment address and offset address.  Complex pointer: Pointers with multiple levels of indirection.  Near pointer: Near pointer is used to store 16-bit addresses means within the current segment on a 16-bit machine.  Normalized pointer: It is a 32-bit pointer, which has as much of its value in the segment register as possible.  File Pointer: The pointer to a FILE data type is called a stream pointer or a file pointer.
  • 32. Size of Pointers in C  The size of the pointers in C is equal for every pointer type.  The size of the pointer does not depend on the type it is pointing to. It only depends on the operating system and CPU architecture.  The size of pointers in C is  8 bytes for a 64-bit System  4 bytes for a 32-bit System
  • 33. C Program to find the size of different pointer types. #include <stdio.h> struct str { }; // dummy function void func(int a, int b){}; int main() { // dummy variables definitions int a = 10; char c = 'G'; struct str x; // pointer definitions of different types int* ptr_int = &a; char* ptr_char = &c; struct str* ptr_str = &x; void (*ptr_func)(int, int) = &func; void* ptr_vn = NULL; // printing sizes printf("Size of Integer Pointer t:t%d bytesn", sizeof(ptr_int)); printf("Size of Character Pointert:t%d bytesn", sizeof(ptr_char)); printf("Size of Structure Pointert:t%d bytesn", sizeof(ptr_str)); printf("Size of Function Pointert:t%d bytesn", sizeof(ptr_func)); printf("Size of NULL Void Pointert:t%d bytes", sizeof(ptr_vn)); return 0; } OUTPUT Size of Integer Pointer : 8 bytes Size of Character Pointer : 8 bytes Size of Structure Pointer : 8 bytes Size of Function Pointer : 8 bytes Size of NULL Void Pointer : 8 bytes
  • 34. POINTER ARITHMETIC • Arithmetic operation supported pointers are – Increment – Decrement – Adding integer value with pointer – Subtracting integer value with pointer • The following arithmetic operations are not allowed with pointers – Multiplication – Division – Add or subtract float/double with pointers… N.Rajkumar 34
  • 35. • Increment: int x=10, *p; p=&x; p++; • p++ will not add 1 to p, but add size of that data type with p, which is pointed by the pointer • If p is 1000(Addr of x) after the statement p=&x, then after p++, p will be 1004, because the size of x is 4 bytes N.Rajkumar 35
  • 36. • Decrement: int x, *p1,*p2,*p3; p1=&x; p2=p1--; p3=p1--; • p1-- will not decrement 1 to p1, but subtract the size of that data type with p1, which is pointed by the pointer • If p1 is 1000(Addr of x) after the statement p1=&x then: • After the statement p2=p1--, p2=998 • After the statement p3=p1--, p3=996 N.Rajkumar 36
  • 37. #include<stdio.h> #include<conio.h> void main() { int x=100; int *ptr; ptr=&x; clrscr(); printf(“Address of x before increment = %u”,ptr); ptr++; printf (“Address of x after increment = %u”,ptr); printf (“Address of x before decrement = %u”,ptr); ptr--; printf(“Address of x after decrement = %u”,ptr); getch(); } N.Rajkumar 37 //Program to display the memory address of a variable using pointers, before and after increment and decrement
  • 38. • Adding integer value with pointer: int *p,x; x=10; p=&x; p=p+7; • If p is 1000(Addr of x), then after the statement p=p+7, p will be 1014. • Subtracting integer value with pointer: int *p,x; x=10; p=&x; p=p-7; • If p is 1000(Addr of x), then after the statement p=p-7, p will be 986. N.Rajkumar 38
  • 39. #include<stdio.h> #include<conio.h> void main() { int x, *p1, *p2, *p3; x=20; p1=&x; p2=p1+7; p3=p1-7; printf(“Value of p1=%d”, *p1); printf(“nAddress of p1=%u”, p1); printf(“nAddress of p2=% u”, p2); printf(“nAddress of p3=% u”, p3); getch(); } N.Rajkumar 39 //Program for adding and subtracting integer value with pointer
  • 40. Pointer to an Array N.Rajkumar 40 #include<stdio.h> #include<string.h> int main() { int a[5]={1,2,3,4,5}; int* p,i; char *p1; char name[]="Rajkumar"; p=&a; p1=&name; for(i=0;i<5;i++,p++) printf("%dn",*p); for(i=0;i<strlen(name);i++,p1++) printf("%c",*p1); }
  • 41. N.Rajkumar 41 #include<stdio.h> int main() { int a[30]; int i,sum=0,*p,n; printf("Enter the no of elements in array:"); scanf("%d",&n); printf("Enter the array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); p=&a; for(i=0;i<n;i++,p++) sum=sum+*p; printf("sum of array elements =%d ",sum); }
  • 42. N.Rajkumar 42 //Find Minimum and Maximum #include<stdio.h> int main() { int a[]={31,21,13,44,51,16,17,28}; int *p,i=30,max,min; p=a; max=min=*p; printf("p=%d",p); for(i=0;i<8;i++,p++) { if(*p>max) max=*p; if(*p<min) min=*p; } printf("max=%d min=%dn",&max,&min); }
  • 43. N.Rajkumar 43 //Find Minimum and Maximum #include<stdio.h> int main() { int a[]={31,21,13,44,51,16,17,28}; int *p,i=30,max,min; p=a; max=min=*p; printf("p=%d",p); for(i=0;i<8;i++,p++) { if(*p>max) max=*p; if(*p<min) min=*p; } printf("max=%d min=%dn",&max,&min); }
  • 44. N.Rajkumar 44 //Example for Pointer to structure #include<stdio.h> struct student { int vtuno; char name[30]; }; int main() { struct student s[10]; struct student *p; int i; printf("Enter the student Details:"); for(i=0;i<3;i++) { printf("Enter the vtuno:"); scanf("%d",&s[i].vtuno); printf("Enter the student name:"); scanf("%s",s[i].name); } p=&s; for(i=0;i<3;i++,p++) { printf("%dt%sn",p->vtuno,p->name); } }
  • 45. Parameter Passing Methods • Call by value • Call by reference 45 N.Rajkumar
  • 46. Call by value • Actual argument passed to the formal argument. • Any changes to the formal argument does not affect the actual argument. 46 N.Rajkumar
  • 47. Example #include <stdio.h> #include<conio.h> int add(int,int); int main() { int a,b,c; clrscr(); printf("nEnter two number:"); scanf("%d%d",&a,&b); c=add(a,b); printf("nSum is:%d",c); } int add(int x,int y) { int z; z=x+y; x=x+20; return(z); } 47 N.Rajkumar
  • 48. Output Enter two number:6 7 Sum is:13 48 N.Rajkumar
  • 49. Call by reference • Instead of passing value, the address of the argument will be passed. • Any changes to the formal argument will affect the actual argument. 49 N.Rajkumar
  • 50. Example #include <stdio.h> #include<conio.h> void swap(int*,int*); void main() { int x,y; printf("nEnter value of x:"); scanf("%d",&x); printf("nEnter value of y:"); scanf("%d",&y); Output Enter value of x:5 Enter value of y:6 x=6,y=5 swap(&x,&y); printf("nx=%d,y=%d",x,y); } void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; printf("nx=%d,y=%d",*a,*b); } 50 N.Rajkumar