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.
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;
}
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.
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
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
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
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
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);
}
}
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
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