SlideShare a Scribd company logo
CLASS:FYBSC(CS)-E
SUBJECT: “ADVANCE C PROGRAMMING”
SEM-II
SYLLABUS: ”Advance C Programming” SEM-II
CH.1 POINTERS
CH.2 STRINGS
CH.3 STRUCTURES AND UNION
CH.4 FILE HANDLING
CH.5 PREPROCESSOR
Chapter 1 :Pointers
• Chapter 1 :Pointers
• 1.1. Introduction to Pointers.
• 1.2. Declaration, definition, initialization, dereferencing.
• 1.3. Pointer arithmetic.
• 1.4. Relationship between Arrays & Pointers- Pointer to array, Array of
pointers.
• 1.5. Multiple indirection (pointer to pointer).
• 1.6. Functions and pointers- Passing pointer to function, Returning pointer
from function, Function pointer.
• 1.7. Dynamic memory management- Allocation(malloc(),calloc()),
Resizing(realloc()), Releasing(free()).,
• 1.8. Memory leak, dangling pointers.
• 1.9. Types of pointers.
CH.1 POINTERS
INTRODUCTION:
• Pointers in C are easy and fun to learn.
• Some C programming tasks are performed more easily with
pointers, and other tasks, such as dynamic memory allocation,
cannot be performed without using pointers.
• So it becomes necessary to learn pointers to become a perfect C
programmer.
INTRODUCTION:
continue…
• The pointer in C language is a variable which stores the
address of another variable.
• This variable can be of type int, char, array, function, or
any other pointer.
• The size of the pointer depends on the architecture.
However, in 32-bit architecture the size of a pointer is 2
byte.
• As you know, every variable is a memory location and every
memory location has its address defined which can be
accessed using ampersand (&) operator, which denotes an
address in memory.
Example:
#include <stdio.h>
int main ()
{
int var1;
char var2[10];
printf("Address of var1 variable: %xn", &var1 );
printf("Address of var2 variable: %xn", &var2 );
return 0; }
Definition,Declaration,Initialization,
Dereferencing of pointers.
• Definition of pointers:
• A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location. Like any
variable or constant, you must declare a pointer before using it
to store any variable address.
• Other Definitions:
1.Pointer is a variable that holds memory address.
2.Pointer is a variable which holds the address of another
variable. It contains the memory location of the variable instead
of its content.
How to Use Pointers
• There are a few important operations, which we will do with the
help of pointers very frequently.
• (a) We define a pointer variable,
• (b) assign the address of a variable to a pointer and
• (c) finally access the value at the address available in the
pointer variable.
• This is done by using unary operator * that returns the value of
the variable located at the address specified by its operand.
Declaration of Pointer
• The pointer in c language can be declared using *
(asterisk symbol). It is also known as indirection
pointer used to dereference a pointer.
1.int *a;//pointer to int
2.char *c;
3.float *d//pointer to float
Example 1:
#include<stdio.h>
int main()
{
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x n",p);
// p contains the address of the number therefore printing p gives the address of number
.
printf("Value of p variable is %d n",*p);
//Here we will get the value stored at the address contained by p.
return 0;
}
Initialization of Pointers
• When we declare a pointer variable it contains garbage value.
i.e. It may be pointing anywhere in the memory. So we should always
assign an address before using it in the program.
• Pointer initialization is the process of assigning address of a variable
to a pointer variable. Pointer variable can only contain address of the
same data type.
• Syntax:
• <pointer declaration>
• Pointer_name =&variable<address of avariable>
Initialization of Pointers-Example
EX1:
Int n,*iptr;
iPtr=&n;
Ex2:
Int a=30,*ptr;
Ptr=&a;
We can also initialize the pointer at the time of declaration.
Ex3:
Int roll=102,*ptr=&roll;
Float sal=5000.00,*fptr=&sal;
sizeof() operator in C
• The sizeof() operator is commonly used in C.
• It determines the size of the expression or the data
type specified in the number of char-sized storage
units.
• The sizeof() operator contains a single operand which
can be either an expression or a data typecast where
the cast is data type enclosed within parenthesis.
• The data type cannot only be primitive data types such
as integer or floating data types, but it can also be
pointer data types and compound data types such as
unions and structs.
Example:
1. sizeof(datatype)
2. Sizeof(pointer variable)
#include <stdio.h>int main()
{ char a='A'; char *ptr; ptr=&a; printf("n sizeof(ptr)=%d",
sizeof(ptr)); printf("nsizeof(a)=%d",sizeof(a));
printf("nsizeof(*ptr)=%d",sizeof(*ptr)); return 0;}
Use of sizeof() operator
• we dynamically allocate the array space by
using sizeof() operator:
• Example:
• int *ptr=malloc(10*sizeof(int));
• We use malloc() function to allocate the memory and
returns the pointer which is pointing to this allocated
memory. The memory space is equal to the number of
bytes occupied by the int data type and multiplied by
10.
Dereferencing of Pointers:
• When indirection operator (*) is used with the pointer
variable, then it is known as dereferencing a
pointer.
• When we dereference a pointer, then the value of the
variable pointed by this pointer will be returned.
Differencing Pointer or Subtraction of pointer
from another pointer:
• Pointer variable can be subtracted from another pointer variable only
if they point to the elements of the same array.
• Syntax:
• Ptr_variable2-ptr_variable1
Why we use dereferencing pointer?
• Dereference a pointer is used because of the
following reasons:
• It can be used to access or manipulate the data stored
at the memory location, which is pointed by the pointer.
• Any operation applied to the dereferenced pointer will
directly affect the value of the variable that it points to.
Example: Dereferencing of pointer
#include <stdio.h>
int main()
{
int x=9;
int *ptr;
ptr=&x;
*ptr=8; //*ptr will update the value of x
printf("value of x is : %d", x);
return 0;
}
Example 2:
#include <stdio.h>
int main()
{
int a=90;
int *ptr1,*ptr2;
ptr1=&a;
ptr2=&a;
*ptr1=7;
*ptr2=6;
printf("The value of a is : %d",a);
return 0;
}
Pointer Arithmetic
• We can perform arithmetic operations on the pointers
like addition, subtraction, etc.
• However, as we know that pointer contains the address,
the result of an arithmetic operation performed on the
pointer will also be a pointer if the other operand is of
type integer.
• In pointer-from-pointer subtraction, the result will be
an integer value.
Following arithmetic operations are
possible on the pointer in C language:
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
Incrementing Pointer in C
• If we increment a pointer by 1, the pointer will start
pointing to the immediate next location.
• This is somewhat different from the general arithmetic
since the value of the pointer will get increased by the
size of the data type.
• We can traverse an array by using the increment
operation on a pointer which will keep pointing to every
element of the array, perform some operation on that,
and update itself in a loop.
The Rule to increment the pointer is
given below:
Syntax:
new_address= current_address + i * size_of(data type)
• Where i is the number by which the pointer get
increased.
• pe to which the pointer is pointing.
Example: Incrementing a pointer Variable:
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p+1;
printf("After increment: Address of p variable is %u n",p); // in our cas
e, p will get incremented by 4 bytes.
return 0;
}
Traversing an array by using pointer
#include<stdio.h>
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int i;
printf("printing array elements...n");
for(i = 0; i< 5; i++)
{
printf("%d ",*(p+i));
}
}
Decrementing Pointer in C
• Like increment, we can decrement a pointer variable. If we
decrement a pointer, it will start pointing to the previous
location.
• The formula of decrementing the pointer is given
below:
new_address= current_address - i * size_of(data type)
• 32-bit
• For 32-bit int variable, it will be decremented by 2 bytes.
• 64-bit
• For 64-bit int variable, it will be decremented by 4 bytes.
Example: Decrementing of pointer
#include <stdio.h>
void main()
{
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p-1;
printf("After decrement: Address of p variable is %u n",p); /
/ P will now point to the immediate previous location.
}
C Pointer Addition
• We can add a value to the pointer variable. The formula
of adding value to pointer is given below:
new_address= current_address + (number * size_of(dat
a type))
• 32-bit
• For 32-bit int variable, it will add 2 * number.
• 64-bit
• For 64-bit int variable, it will add 4 * number.
C Pointer Addition
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u n",p);
return 0;
}
C Pointer Subtraction
• Like pointer addition, we can subtract a value from the
pointer variable. Subtracting any number from a pointer will
give an address.
• The formula of subtracting value from the pointer
variable is given below:
new_address= current_address -
(number * size_of(data type))
• 32-bit
• For 32-bit int variable, it will subtract 2 * number.
• 64-bit
• For 64-bit int variable, it will subtract 4 * number.
Example:
#include<stdio.h>
int main()
{
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u n",p);
return 0;
}
Comparison between pointers:
Following operations that can never be
performed on pointers are:
• 1. Addition,Multiplication,Division of two pointers.
• 2.Multiplication between pointer and any number.
• 3.Division of pointer by any number.
• Addition of float or double values to the pointer.
Relationship between Arrays and Pointers
• In C language ,there is a very strong relationship between pointers
and arrays .Arrays can created as pointers and vice versa.
• Following are the main points for understanding the relationship of
pointers with arrays:
1.Elements of an array are stored in consecutive memory locations.
2.The name of an array is a constant pointer that points to the first
element of the array. i.e it stores the address of the first element, also
known as the base address of array.
3.According to pointer arithmetic, when a pointer variable is
incremented, it points to the next location of its base type.
Example:
• Int arr[5]; 1000 1004 1008 1012 1016----------1020
• Int *ptr1; //normal pointer
• Ptr1=&arr[0];
• Ptr1++;
• Int arr[5];
• Int *ptr2[5];// pointer to array
• Ptr2=&arr;//point to the complete
• Ptr2++;
EXAMPLE:
• Int ptrarray[5]={10,11,12,13,14};
HERE, ptrarray and & ptrarray[0] represents ??
ANS: SAME ADDRESS
Pointer To Array:
• It is possible to create a pointer that points to a complete array
instead of pointing to the individual element of an array such pointer
is known as pointer to an array.
• NORMAL POINTER
• EX: PROGRAM
• POINTER TO ARRAY
• EX PROGRAM
Multiple indirection(Double pointer)
• If you define a pointer to pointer;
• WHAT WILL HAPPEN?
The first pointer is used to store the address of the variable
and second pointer is used to store the address of the first
pointer.
Int x=10; Int *ptr1; ptr1=&x;
int **ptr; Ptr=&ptr1;
Declaration: int **ptr;
EXAMPLE:
1.Program to find length of the string.
2.Program to compare strings using pointers.
3.Program to write Function to sort the numbers using pointers.
DYNAMIC MEMORY ALLOCATION: Concept
DMA: Dynamic Memory Allocation
• C Dynamic Memory Allocation can be defined as a procedure
in which the size of a data structure (like Array) is changed
during the runtime.
• C provides some functions to achieve these tasks. There are 4
library functions provided by C defined under <stdlib.h> header
file to facilitate dynamic memory allocation in C programming.
They are:
1.malloc()
2.calloc()
3.free()
4.realloc()
malloc():
• “malloc” or “memory allocation” method in C is used to
dynamically allocate a single large block of memory with the
specified size.
• It returns a pointer of type void which can be cast into a pointer of
any form. It initializes each block with default garbage value.
• Syntax:
ptr = (cast-type*) malloc(byte-size)
Example: ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the
address of the first byte in the allocated memory.
calloc():
• calloc” or “contiguous allocation” method in C is used to
dynamically allocate the specified number of blocks of memory
of the specified type.
• It initializes each block with a default value ‘0’.
Syntax: ptr = (float*) calloc(25, sizeof(float));
• This statement allocates contiguous space in memory for 25
elements each with the size of the float.
DMA:EXAMPLE
• #include <stdio.h>#include<stdlib.h>main(){ int i; int
*ptr=(int*)malloc(2*sizeof(int)); //allocate 2 elements
if(ptr==NULL) { printf("memory is not available"); exit(1); }
printf("enter 2 no's:n"); for(i=0;i<2;i++) { scanf("%d",ptr+i); }
ptr=(int*) realloc(ptr,4*sizeof(int)); if(ptr==NULL) {
printf("memory is not available"); exit(1); } printf("enter 2
no's:n"); for(i=2;i<4;i++) //change in the for loop {
scanf("%d",ptr+i); } for(i=0;i<4;i++) //change in the for loop
{ printf("%d",*(ptr+i)); } return 0;
free():
• “free” method in C is used to dynamically de-allocate the
memory.
• The memory allocated using functions malloc() and calloc() is
not de-allocated on their own.
• Hence the free() method is used, whenever the dynamic
memory allocation takes place. It helps to reduce wastage of
memory by freeing it.
• Syntax: free(ptr())
THINGS TO DO : TO MAKE YOURSELF HAPPY
AND POSITIVE
THANK YOU

More Related Content

PPTX
Pointer arithmetic in c
PPTX
Demultiplexer presentation
PPTX
Presentation bcd adder
PPT
Pointers in c
PPTX
Pointers in c - Mohammad Salman
PPT
Chelsea mourinho bible 39 1st team drills
PPTX
SHIFT REGISTERS
PPT
Chp6 assembly language programming for pic copy
Pointer arithmetic in c
Demultiplexer presentation
Presentation bcd adder
Pointers in c
Pointers in c - Mohammad Salman
Chelsea mourinho bible 39 1st team drills
SHIFT REGISTERS
Chp6 assembly language programming for pic copy

What's hot (20)

PPTX
Flip flop conversions
DOC
How to do football match analysis
PPTX
Logical instructions (and, or, xor, not, test)
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Report procedure
PPTX
Verilog data types -For beginners
PDF
Lesson 03 python statement, indentation and comments
PDF
Rondos - Up Back Through
PDF
Verilog coding of demux 8 x1
PPTX
C Programming : Pointers and Arrays, Pointers and Strings
DOCX
All flipflop
PDF
Pointers in C
PDF
8086 instruction set with types
PPTX
Pointers in C
PPT
PPT
Strings in c
PPTX
Operators and expressions in c language
PPTX
80386-1.pptx
PPT
Preprocessor in C
PPTX
The Theory of the Xcel - Soccer - Coaching Model
Flip flop conversions
How to do football match analysis
Logical instructions (and, or, xor, not, test)
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Report procedure
Verilog data types -For beginners
Lesson 03 python statement, indentation and comments
Rondos - Up Back Through
Verilog coding of demux 8 x1
C Programming : Pointers and Arrays, Pointers and Strings
All flipflop
Pointers in C
8086 instruction set with types
Pointers in C
Strings in c
Operators and expressions in c language
80386-1.pptx
Preprocessor in C
The Theory of the Xcel - Soccer - Coaching Model
Ad

Similar to FYBSC(CS)_UNIT-1_Pointers in C.pptx (20)

PPSX
Pointers
PPT
pointers (1).ppt
PDF
PSPC--UNIT-5.pdf
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPTX
COM1407: Working with Pointers
PPTX
Pointer.pptx
PPTX
4 Pointers.pptx
PPTX
Chapter 3 summaryv2(Pointers) in c++.pptx
PPTX
PPS-POINTERS.pptx
PPT
Lecture2.ppt
PPTX
2-Concept of Pointers in c programming.pptx
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Chp3(pointers ref)
PPTX
pointers in c programming - example programs
PPT
Pointers C programming
PPTX
20.C++Pointer.pptx
PPTX
Pointers and Array, pointer and String.pptx
PPT
PPT
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
Pointers
pointers (1).ppt
PSPC--UNIT-5.pdf
Pointers and single &multi dimentionalarrays.pptx
COM1407: Working with Pointers
Pointer.pptx
4 Pointers.pptx
Chapter 3 summaryv2(Pointers) in c++.pptx
PPS-POINTERS.pptx
Lecture2.ppt
2-Concept of Pointers in c programming.pptx
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Chp3(pointers ref)
pointers in c programming - example programs
Pointers C programming
20.C++Pointer.pptx
Pointers and Array, pointer and String.pptx
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Ad

More from sangeeta borde (11)

PPTX
CH 4_TYBSC(CS)_Data Science_Visualisation
PDF
Ch.3 Data Science Data Preprocessing.pdf
PDF
Data Science_Chapter -2_Statical Data Analysis.pdf
PPTX
Ch1_Introduction to DATA SCIENCE_TYBSC(CS)_2024.pptx
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PPTX
CH2_CYBER_SECURITY_FYMSC(DS)-MSC(CS)-MSC(IMCA).pptx
PPTX
UNIT-5_Array in c_part1.pptx
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
3. Test Scenarios & Test Cases with Excel Sheet Format (1).pdf
PPTX
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
PPTX
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx
CH 4_TYBSC(CS)_Data Science_Visualisation
Ch.3 Data Science Data Preprocessing.pdf
Data Science_Chapter -2_Statical Data Analysis.pdf
Ch1_Introduction to DATA SCIENCE_TYBSC(CS)_2024.pptx
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
CH2_CYBER_SECURITY_FYMSC(DS)-MSC(CS)-MSC(IMCA).pptx
UNIT-5_Array in c_part1.pptx
CH.4FUNCTIONS IN C (1).pptx
3. Test Scenarios & Test Cases with Excel Sheet Format (1).pdf
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx

Recently uploaded (20)

DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Trump Administration's workforce development strategy
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
History, Philosophy and sociology of education (1).pptx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Paper A Mock Exam 9_ Attempt review.pdf.
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Trump Administration's workforce development strategy
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
History, Philosophy and sociology of education (1).pptx

FYBSC(CS)_UNIT-1_Pointers in C.pptx

  • 2. SYLLABUS: ”Advance C Programming” SEM-II CH.1 POINTERS CH.2 STRINGS CH.3 STRUCTURES AND UNION CH.4 FILE HANDLING CH.5 PREPROCESSOR
  • 3. Chapter 1 :Pointers • Chapter 1 :Pointers • 1.1. Introduction to Pointers. • 1.2. Declaration, definition, initialization, dereferencing. • 1.3. Pointer arithmetic. • 1.4. Relationship between Arrays & Pointers- Pointer to array, Array of pointers. • 1.5. Multiple indirection (pointer to pointer). • 1.6. Functions and pointers- Passing pointer to function, Returning pointer from function, Function pointer. • 1.7. Dynamic memory management- Allocation(malloc(),calloc()), Resizing(realloc()), Releasing(free())., • 1.8. Memory leak, dangling pointers. • 1.9. Types of pointers.
  • 5. INTRODUCTION: • Pointers in C are easy and fun to learn. • Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. • So it becomes necessary to learn pointers to become a perfect C programmer.
  • 6. INTRODUCTION: continue… • The pointer in C language is a variable which stores the address of another variable. • This variable can be of type int, char, array, function, or any other pointer. • The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte. • As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.
  • 7. Example: #include <stdio.h> int main () { int var1; char var2[10]; printf("Address of var1 variable: %xn", &var1 ); printf("Address of var2 variable: %xn", &var2 ); return 0; }
  • 8. Definition,Declaration,Initialization, Dereferencing of pointers. • Definition of pointers: • A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. • Other Definitions: 1.Pointer is a variable that holds memory address. 2.Pointer is a variable which holds the address of another variable. It contains the memory location of the variable instead of its content.
  • 9. How to Use Pointers • There are a few important operations, which we will do with the help of pointers very frequently. • (a) We define a pointer variable, • (b) assign the address of a variable to a pointer and • (c) finally access the value at the address available in the pointer variable. • This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
  • 10. Declaration of Pointer • The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer. 1.int *a;//pointer to int 2.char *c; 3.float *d//pointer to float
  • 11. Example 1: #include<stdio.h> int main() { int number=50; int *p; p=&number;//stores the address of number variable printf("Address of p variable is %x n",p); // p contains the address of the number therefore printing p gives the address of number . printf("Value of p variable is %d n",*p); //Here we will get the value stored at the address contained by p. return 0; }
  • 12. Initialization of Pointers • When we declare a pointer variable it contains garbage value. i.e. It may be pointing anywhere in the memory. So we should always assign an address before using it in the program. • Pointer initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of the same data type. • Syntax: • <pointer declaration> • Pointer_name =&variable<address of avariable>
  • 13. Initialization of Pointers-Example EX1: Int n,*iptr; iPtr=&n; Ex2: Int a=30,*ptr; Ptr=&a; We can also initialize the pointer at the time of declaration. Ex3: Int roll=102,*ptr=&roll; Float sal=5000.00,*fptr=&sal;
  • 14. sizeof() operator in C • The sizeof() operator is commonly used in C. • It determines the size of the expression or the data type specified in the number of char-sized storage units. • The sizeof() operator contains a single operand which can be either an expression or a data typecast where the cast is data type enclosed within parenthesis. • The data type cannot only be primitive data types such as integer or floating data types, but it can also be pointer data types and compound data types such as unions and structs.
  • 15. Example: 1. sizeof(datatype) 2. Sizeof(pointer variable) #include <stdio.h>int main() { char a='A'; char *ptr; ptr=&a; printf("n sizeof(ptr)=%d", sizeof(ptr)); printf("nsizeof(a)=%d",sizeof(a)); printf("nsizeof(*ptr)=%d",sizeof(*ptr)); return 0;}
  • 16. Use of sizeof() operator • we dynamically allocate the array space by using sizeof() operator: • Example: • int *ptr=malloc(10*sizeof(int)); • We use malloc() function to allocate the memory and returns the pointer which is pointing to this allocated memory. The memory space is equal to the number of bytes occupied by the int data type and multiplied by 10.
  • 17. Dereferencing of Pointers: • When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer. • When we dereference a pointer, then the value of the variable pointed by this pointer will be returned.
  • 18. Differencing Pointer or Subtraction of pointer from another pointer: • Pointer variable can be subtracted from another pointer variable only if they point to the elements of the same array. • Syntax: • Ptr_variable2-ptr_variable1
  • 19. Why we use dereferencing pointer? • Dereference a pointer is used because of the following reasons: • It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer. • Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.
  • 20. Example: Dereferencing of pointer #include <stdio.h> int main() { int x=9; int *ptr; ptr=&x; *ptr=8; //*ptr will update the value of x printf("value of x is : %d", x); return 0; }
  • 21. Example 2: #include <stdio.h> int main() { int a=90; int *ptr1,*ptr2; ptr1=&a; ptr2=&a; *ptr1=7; *ptr2=6; printf("The value of a is : %d",a); return 0; }
  • 22. Pointer Arithmetic • We can perform arithmetic operations on the pointers like addition, subtraction, etc. • However, as we know that pointer contains the address, the result of an arithmetic operation performed on the pointer will also be a pointer if the other operand is of type integer. • In pointer-from-pointer subtraction, the result will be an integer value.
  • 23. Following arithmetic operations are possible on the pointer in C language: • Increment • Decrement • Addition • Subtraction • Comparison
  • 24. Incrementing Pointer in C • If we increment a pointer by 1, the pointer will start pointing to the immediate next location. • This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type. • We can traverse an array by using the increment operation on a pointer which will keep pointing to every element of the array, perform some operation on that, and update itself in a loop.
  • 25. The Rule to increment the pointer is given below: Syntax: new_address= current_address + i * size_of(data type) • Where i is the number by which the pointer get increased. • pe to which the pointer is pointing.
  • 26. Example: Incrementing a pointer Variable: #include<stdio.h> int main() { int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p+1; printf("After increment: Address of p variable is %u n",p); // in our cas e, p will get incremented by 4 bytes. return 0; }
  • 27. Traversing an array by using pointer #include<stdio.h> #include<stdio.h> void main () { int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; int i; printf("printing array elements...n"); for(i = 0; i< 5; i++) { printf("%d ",*(p+i)); } }
  • 28. Decrementing Pointer in C • Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. • The formula of decrementing the pointer is given below: new_address= current_address - i * size_of(data type) • 32-bit • For 32-bit int variable, it will be decremented by 2 bytes. • 64-bit • For 64-bit int variable, it will be decremented by 4 bytes.
  • 29. Example: Decrementing of pointer #include <stdio.h> void main() { int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p-1; printf("After decrement: Address of p variable is %u n",p); / / P will now point to the immediate previous location. }
  • 30. C Pointer Addition • We can add a value to the pointer variable. The formula of adding value to pointer is given below: new_address= current_address + (number * size_of(dat a type)) • 32-bit • For 32-bit int variable, it will add 2 * number. • 64-bit • For 64-bit int variable, it will add 4 * number.
  • 31. C Pointer Addition #include<stdio.h> int main() { int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p+3; //adding 3 to pointer variable printf("After adding 3: Address of p variable is %u n",p); return 0; }
  • 32. C Pointer Subtraction • Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address. • The formula of subtracting value from the pointer variable is given below: new_address= current_address - (number * size_of(data type)) • 32-bit • For 32-bit int variable, it will subtract 2 * number. • 64-bit • For 64-bit int variable, it will subtract 4 * number.
  • 33. Example: #include<stdio.h> int main() { int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p-3; //subtracting 3 from pointer variable printf("After subtracting 3: Address of p variable is %u n",p); return 0; }
  • 35. Following operations that can never be performed on pointers are: • 1. Addition,Multiplication,Division of two pointers. • 2.Multiplication between pointer and any number. • 3.Division of pointer by any number. • Addition of float or double values to the pointer.
  • 36. Relationship between Arrays and Pointers • In C language ,there is a very strong relationship between pointers and arrays .Arrays can created as pointers and vice versa. • Following are the main points for understanding the relationship of pointers with arrays: 1.Elements of an array are stored in consecutive memory locations. 2.The name of an array is a constant pointer that points to the first element of the array. i.e it stores the address of the first element, also known as the base address of array. 3.According to pointer arithmetic, when a pointer variable is incremented, it points to the next location of its base type.
  • 37. Example: • Int arr[5]; 1000 1004 1008 1012 1016----------1020 • Int *ptr1; //normal pointer • Ptr1=&arr[0]; • Ptr1++; • Int arr[5]; • Int *ptr2[5];// pointer to array • Ptr2=&arr;//point to the complete • Ptr2++;
  • 38. EXAMPLE: • Int ptrarray[5]={10,11,12,13,14}; HERE, ptrarray and & ptrarray[0] represents ?? ANS: SAME ADDRESS
  • 39. Pointer To Array: • It is possible to create a pointer that points to a complete array instead of pointing to the individual element of an array such pointer is known as pointer to an array. • NORMAL POINTER • EX: PROGRAM • POINTER TO ARRAY • EX PROGRAM
  • 40. Multiple indirection(Double pointer) • If you define a pointer to pointer; • WHAT WILL HAPPEN? The first pointer is used to store the address of the variable and second pointer is used to store the address of the first pointer. Int x=10; Int *ptr1; ptr1=&x; int **ptr; Ptr=&ptr1; Declaration: int **ptr;
  • 41. EXAMPLE: 1.Program to find length of the string. 2.Program to compare strings using pointers. 3.Program to write Function to sort the numbers using pointers.
  • 43. DMA: Dynamic Memory Allocation • C Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime. • C provides some functions to achieve these tasks. There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming. They are: 1.malloc() 2.calloc() 3.free() 4.realloc()
  • 44. malloc(): • “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. • It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with default garbage value. • Syntax: ptr = (cast-type*) malloc(byte-size) Example: ptr = (int*) malloc(100 * sizeof(int)); Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.
  • 45. calloc(): • calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. • It initializes each block with a default value ‘0’. Syntax: ptr = (float*) calloc(25, sizeof(float)); • This statement allocates contiguous space in memory for 25 elements each with the size of the float.
  • 46. DMA:EXAMPLE • #include <stdio.h>#include<stdlib.h>main(){ int i; int *ptr=(int*)malloc(2*sizeof(int)); //allocate 2 elements if(ptr==NULL) { printf("memory is not available"); exit(1); } printf("enter 2 no's:n"); for(i=0;i<2;i++) { scanf("%d",ptr+i); } ptr=(int*) realloc(ptr,4*sizeof(int)); if(ptr==NULL) { printf("memory is not available"); exit(1); } printf("enter 2 no's:n"); for(i=2;i<4;i++) //change in the for loop { scanf("%d",ptr+i); } for(i=0;i<4;i++) //change in the for loop { printf("%d",*(ptr+i)); } return 0;
  • 47. free(): • “free” method in C is used to dynamically de-allocate the memory. • The memory allocated using functions malloc() and calloc() is not de-allocated on their own. • Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it. • Syntax: free(ptr())
  • 48. THINGS TO DO : TO MAKE YOURSELF HAPPY AND POSITIVE