PPS Unit 04
PPS Unit 04
• A variable of structure type can store multiple data items of different data
types under one name.
struct <struct_name>
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000 emp_id
8002 name[20]
8022 salary
8026 address[50]
8076 dept_no
8078 age
Example for sizeof() structure
#include<stdio.h>
struct stud
{
int roll;
char name[10];
int marks;
};
void main()
{
int size;
struct stud s; //Declaring a structure variable
size = sizeof(s);
printf(“\nSize of Structure : %d", size);
}
Size of Structure 'S' = sizeof(roll) + sizeof(name) + sizeof(mark)
= 2 + 10 + 2 = 14
Accessing a Structure Members
2) s.rollno=111
s.name=“Sanjana”
s.marks=558
Example program for structure Initialization
#include<stdio.h>
struct stud
{
int roll;
char name[10];
int marks;
};
void main()
{
int size;
struct stud s={111,"Sanjana",558};
printf("Roll no : %d\n", s.roll);
printf("Name : %s\n", s.name);
printf("Marks : %d\n", s.marks);
}
Example program to read data from keyboard
#include <stdio.h>
struct student scanf("%d", &s.roll);
{ printf("Enter marks: ");
char name[50]; scanf("%f", &s.marks);
int roll; printf("Displaying Information:\
float marks; n");
} s; printf("Name: ");
void main() puts(s.name);
{ printf("Roll number: %d\
printf("Enter information:\n"); n",s.roll);
printf("Enter name: "); printf("Marks: %f\n", s.marks);
scanf("%s", s.name); }
printf("Enter roll number: ");
Structure Assignment
The value of one structure variable is assigned to another
variable of same type using assignment statement. If the e1 and
e2 are structure variables of type employee then the statement
e1 = e2;
For Example:-
struct employee e1[100];
Program to implement the Array of Structure
#include<stdio.h>
struct Employee scanf("%d",&Emp[i].Id);
{ printf("\n\tEnter Employee Name : ");
int Id; scanf("%s",&Emp[i].Name);
char Name[25]; printf("\n\tEnter Employee Age : ");
int Age; scanf("%d",&Emp[i].Age);
long Salary; printf("\n\tEnter Employee Salary : ");
}; scanf("%ld",&Emp[i].Salary);
void main() }
{ printf("\nDetails of Employees");
int i; for(i=0;i<3;i++)
struct Employee Emp[ 3 ]; printf("\n%d\t%s\t%d\t
for(i=0;i<3;i++) %ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,
{ Emp[i].Salary);
printf("\nEnter details of %d }
Employee",i+1);
printf("\n\tEnter Employee
Id : ");
Structures within Structures
{
<data_type> <variable_name>;
struct <struct_name>
{
<data_type> <variable_name>;
……..
}<struct_variable>;
} <variable_name>;
Structures within Structures Example
struct employee
For Example:-
e1.doj.day;
e1.doj.month;
e1.doj.year;
Accessing Structures within Structures
#include <stdio.h> {
#include <string.h> e1.id=101;
struct Employee strcpy(e1.name, "Sonoo
{ Jaiswal");//copying string into char array
int id; e1.doj.dd=10;
char name[20]; e1.doj.mm=11;
struct Date e1.doj.yyyy=2014;
{ printf( "employee id : %d\n", e1.id);
int dd; printf( "employee name : %s\n",
int mm; e1.name);
int yyyy; printf( "employee date of joining
}doj; (dd/mm/yyyy) : %d/%d/%d\n",
}e1; e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
}
void main( )
Union Data Type
union <union_name>
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Example of Union
The union of Employee is declared as
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Difference between Structures & Union
26
Pointer Basics
Pointer Definition and syntax
•* symbol is used to get the value of the variable that the pointer is
pointing to.
32
Pointer Arithmetic
33
Pointer to Pointer
If a pointer holds the address of another pointer then such type of
pointer is known as pointer-to-pointer or double pointer
Syntax:
int **ptr;
34
Pointer to Pointer Example
#include<stdio.h>
void main()
{
int a, *p1, **p2;
a=65;
p1=&a;
p2=&p1;
printf("a = %d\n", a);//65
printf("address of a = %d\n", &a);//5000
printf("p1 = %d\n", p1);//5000
printf("address p1 = %d\n", &p1);//6000
printf("*p1 = %d\n", *p1);//65
printf("p2 = %d\n", p2);//6000
printf("*p2 = %d\n", *p2);//5000
35
printf("**p2 = %d\n", **p2);//65
}
Pointer to Arrays
• When an array is declared, compiler allocates sufficient amount of
memory to contain all the elements of the array.
• Base address i.e address of the first element of the array is also
allocated by the compiler.
Example:
int x[4];
The program computes the sum of six elements entered by the user.
Example 2: Pointers and Arrays
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2]; // ptr is assigned the address of the third element
printf("*ptr = %d \n", *ptr); // 3
printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
return 0;
}
Pointers as Function Argument in C
• Pointer as a function parameter is used to hold addresses of
arguments passed during function call. This is also known as call
by reference.
• When a function is called by reference any change made to the
reference variable will effect the original variable.
• The address of num1 and num2 are passed to the swap() function
using swap(&num1, &num2);.
Syntax:
enum flag {const1, const2, ..., constN};
1.malloc()
2.calloc()
3.realloc()
4.free()
malloc()
C malloc()
The name "malloc" stands for memory allocation.
The malloc() function reserves a block of memory of the specified number
of bytes. And, it returns a pointer of void which can be casted into pointers
of any form.
Syntax of malloc()
Example
ptr = (float*) malloc(100 * sizeof(float));
The above statement allocates 400 bytes of memory. It's because the size of
float is 4 bytes. And, the pointer ptr holds the address of the first byte in the
allocated memory.
Syntax of calloc()
ptr = (castType*)calloc(n, size);
Example:
ptr = (float*) calloc(25, sizeof(float));
Syntax of realloc()
C free()
Syntax of free()
free(ptr);
int main()
{
int x = 10, y = 5, area;
area = AREA(x, y);
printf("Area of rectangle is: %d", area);
return 0;
}
Output:
Area of rectangle is: 50
#Ifdef, #Else And #Endif Preprocessors
• “#ifdef” directive checks whether particular macro is defined or not.
If it is defined, “If” clause statements are included in source file.
• Otherwise, “else” clause statements are included in source file for
compilation and execution.
Example:
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf("RAJU is defined. So, this line will be added in this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0; Output:
RAJU is defined. So, this line will be added in this C file
}
#Ifndef and #Endif Preprocessors
• #ifndef exactly acts as reverse as #ifdef directive. If particular macro
is not defined, “If” clause statements are included in source file.
• Otherwise, else clause statements are included in source file for
compilation and execution.
#include <stdio.h>
#define RAJU 100
int main()
{
#ifndef SELVA
{
printf("SELVA is not defined. So, now we are going to define here\n");
#define SELVA 300
}
#else
printf("SELVA is already defined in the program”);
#endif
return 0;
Output: SELVA is not defined. So, now we are going to
define here
}
#If, #Else and #Endif Preprocessors
•“If” clause statement is included in source file if given condition is true.
•Otherwise, else clause statement is included in source file for compilation
and execution.
Example:
#include <stdio.h>
#define a 100
int main()
{
#if (a==100)
printf("This line will be added in this C file since a = 100\n");
#else
printf("This line will be added in this C file since a is not equal to 100\n");
#endif
return 0;
}
Output: This line will be added in this C file
since a = 100
#Undef Preprocessor
This directive undefines existing macro in the program.
Example:
#include <stdio.h>
#define height 100
void main()
{
printf("First defined value for height : %d\n",height);
#undef height // undefining variable
#define height 600 // redefining the same for new value
printf("value of height after undef & redefine:%d",height);
}
Output:
First defined value for height : 100
value of height after undef & redefine : 600
“Command Line Arguments”
Command Line Arguments
• The arguments passed from command line are called command line
arguments. These arguments are handled by main() function.
• The argument values are passed on to your program during
program execution.
Syntax:
int main(int argc, char *argv[])
Where,
1.argc: It refers to “argument count”. It counts the number of
arguments on the command line.
2. Write a program.
First use cd.. for coming back to Turbo C++ main directory
cd..
cd SOURCE
Command Line Arguments Execution
6. Execute Program with Command Line Arguments
“Thank You”