SlideShare une entreprise Scribd logo
1  sur  32
Pointers in C
Definition
 A Pointer is a variable that holds
address of another variable of same
data type.
 also known as locator or indicator that
points to an address of a value.
Benefits of using pointers
 Pointer reduces the
code and improves the performance, it
is used to retrieving strings, trees etc.
and used with arrays, structures and
functions.
 We can return multiple values from
function using pointer.
 It makes you able to access any
memory location in the computer's
memory.
 It allows C to support dynamic memory
Concept of pointer
 Whenever a variable is declared in the
program, system allocates a location i.e a
name to that variable in the memory, to hold
the assigned value. This location has its
own address number.
 Let us assume that system has allocated
memory location 80F for a variable a.
int a = 10 ;
 We can access the value 10 by either using the
variable name a or the address 80F. Since the
memory addresses are simply numbers they
can be assigned to some other variable. The
variable that holds memory address are
called pointer variables. A pointer variable is
therefore nothing but a variable that contains
an address, which is a location of another
variable. Value of pointer variable will be
stored in another memory location.
Declaring a pointer variable
 General syntax of pointer declaration
is,
 Data type of a pointer must be same
as the data type of a variable to which
the pointer variable is
pointing. void type pointer works with
all data types, but is not used often
used.
Initialization of Pointer
variable
 Pointer Initialization is the process of
assigning address of a variable
to pointer variable. Pointer variable
contains address of variable of same
data type.
 In C language address operator & is
used to determine the address of a
variable.
 The & (immediately preceding a variable
name) returns the address of the
variable associated with it.
Pointer variable always points to same type of
data.
Note: If you do not have an exact address to be
assigned to a pointer variable while declaration, It
is recommended to assign a NULL value to the
pointer variable. A pointer which is assigned a
NULL value is called a null pointer.
Dereferencing of Pointer
 Once a pointer has been assigned the
address of a variable. To access the
value of variable, pointer is
dereferenced, using the indirection
operator *.
Points to remember:
 While declaring/initializing the pointer
variable, * indicates that the variable is a
pointer.
 The address of any variable is given by
preceding the variable name with
Ampersand '&'.
 The pointer variable stores the address of a
variable. The declaration int *a doesn't mean
that a is going to contain an integer value. It
means that a is going to contain the address
of a variable storing integer value.
 To access the value of a certain address
stored by a pointer variable, * is used. Here,
the * can be read as 'value at'.
A Simple program to explain
pointers:
Pointer Arithmetic
 16 bit Machine ( Turbo C )
In a 16 bit machine, size of all types of
pointer, be
it int*, float*, char* or double* is
always 2 bytes.
But when we perform any arithmetic
function like increment on a pointer,
changes occur as per the size of their
primitive data type.
Examples for Pointer
Arithmetic
In the above case, pointer will be of 2 bytes. And when we increment
it, it will increment by 2 bytes because int is also of 2 bytes.
In this case, size of pointer is still 2 bytes. But now, when we
increment it, it will increment by 4 bytes because float is of 4 bytes.
Similarly, in this case, size of pointer is still 2 bytes. But now, when
we increment it, it will increment by 8 bytes because its data type
is double.
32 bit Machine (Visual Basic
C++)
 The concept of pointer arithmetic
remains exact same, but the size of
pointer and various datatypes is
different in a 32 bit machine. Pointer
in 32 bit machine is of 4 bytes.
 And, following is a table for Size of
datatypes on 32-bit Machine :
 Note: We cannot add two pointers. This is
because pointers contain addresses, adding
two addresses makes no sense, because you
have no idea what it would point to.But we
can subtract two pointers.This is because
difference between two pointers gives the
number of elements of its data type that can
be stored between the two pointers.
Program for pointer
arithmetic(32-bit machine)
 Explanation of the above program:
 Point 1: Here, * means 'value at the given
address'. Thus, it adds the value of m and n
which is 15.
 Point 2: It subtracts the addresses of the two
variables and then divides it by the size of the
pointer datatype (here integer, which has size
of 4 bytes) which gives us the number of
elements of integer data type that can be
stored within it.
 Point 3: It increments the address stored by
the pointer by the size of its datatype(here 4).
 Point 4: It decrements the address stored by
the pointer by the size of its datatype(here 4).
 Point 5: Addition of two pointers is not allowed.
Pointer and 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.
 Suppose we declare an array arr,
 Assuming that the base address of arr is 1000
and each integer requires two bytes, the five
elements will be stored as follows:
 Here variable arr will give the base address,
which is a constant pointer pointing to the
element, arr[0].
 Therefore arr is containing the address
of arr[0] i.e 1000.
 In short, arr has two purpose - it is the name of
an array and it acts as a pointer pointing towards
the first element in the array.
arr is equal to &arr[0] //by default
 We can declare a pointer of type int to
point to the array arr.
 Now we can access every element of
array arr using p++ to move from one
element to another.
NOTE : You cannot decrement a pointer
once incremented. p-- won't work.
Example
void main()
{
int x[5]={2,4,6,8,10},i=0;
clrscr();
printf(“nElement Number Element
Address”);
while(i<5)
{
printf(“nx[%d] = t%d
%u”,i,*(x+i),x+i);
i++;
}
Output
Element Number Element Address
X[0]= 2 4056
X[1]= 4 4058
X[2]= 6 4060
X[3]= 8 4062
X[4]= 10 4064
Pointer to Array
 we can use a pointer to point to an Array,
and then we can use that pointer to
access the array.
 Example:
 In the above program, the pointer *p will
print all the values stored in the array
one by one. We can also use the Base
address (a in above case) to act as
pointer and print all the values.
 The generalized form for using pointer
with an array,
is same as:
Array of pointers
Example, which uses an array of 3
integers −
When the above code is compiled and
executed, it produces the following
result −
 There may be a situation when we
want to maintain an array, which can
store pointers to an int or char or any
other data type available. Following is
the declaration of an array of pointers
to an integer −
 It declares ptr as an array of MAX
integer pointers. Thus, each element
in ptr, holds a pointer to an int value.
 The following example uses three integers,
which are stored in an array of pointers, as
follows −
When the above code is compiled and
executed, it produces the following result −
Pointers to Function
 It is possible to declare a pointer
pointing to a function which can then
be used as an argument in another
function. A pointer to a function is
declared as follows,
 A function pointer can point to a
specific function when it is assigned
the name of the function.
 s is a pointer to a function sum.
Now sum can be called using function
pointer s with the list of parameter.
Example of Pointer to
Function

Contenu connexe

Tendances (20)

C pointer
C pointerC pointer
C pointer
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Strings
StringsStrings
Strings
 
C Pointers
C PointersC Pointers
C Pointers
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
structure and union
structure and unionstructure and union
structure and union
 
Data types in C
Data types in CData types in C
Data types in C
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Similaire à Pointers in c v5 12102017 1

POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxJanineCallangan
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersANUSUYA S
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfmalavshah9013
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 

Similaire à Pointers in c v5 12102017 1 (20)

Pointers
PointersPointers
Pointers
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Pointers
PointersPointers
Pointers
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointer in c
Pointer in c Pointer in c
Pointer in c
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdfDeclaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 

Plus de tanmaymodi4

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c languagetanmaymodi4
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c languagetanmaymodi4
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 

Plus de tanmaymodi4 (11)

Cryptocurrency
CryptocurrencyCryptocurrency
Cryptocurrency
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 

Dernier

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Dernier (20)

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Pointers in c v5 12102017 1

  • 2. Definition  A Pointer is a variable that holds address of another variable of same data type.  also known as locator or indicator that points to an address of a value.
  • 3. Benefits of using pointers  Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions.  We can return multiple values from function using pointer.  It makes you able to access any memory location in the computer's memory.  It allows C to support dynamic memory
  • 4. Concept of pointer  Whenever a variable is declared in the program, system allocates a location i.e a name to that variable in the memory, to hold the assigned value. This location has its own address number.  Let us assume that system has allocated memory location 80F for a variable a. int a = 10 ;
  • 5.  We can access the value 10 by either using the variable name a or the address 80F. Since the memory addresses are simply numbers they can be assigned to some other variable. The variable that holds memory address are called pointer variables. A pointer variable is therefore nothing but a variable that contains an address, which is a location of another variable. Value of pointer variable will be stored in another memory location.
  • 6. Declaring a pointer variable  General syntax of pointer declaration is,  Data type of a pointer must be same as the data type of a variable to which the pointer variable is pointing. void type pointer works with all data types, but is not used often used.
  • 7. Initialization of Pointer variable  Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type.  In C language address operator & is used to determine the address of a variable.  The & (immediately preceding a variable name) returns the address of the variable associated with it.
  • 8. Pointer variable always points to same type of data. Note: If you do not have an exact address to be assigned to a pointer variable while declaration, It is recommended to assign a NULL value to the pointer variable. A pointer which is assigned a NULL value is called a null pointer.
  • 9. Dereferencing of Pointer  Once a pointer has been assigned the address of a variable. To access the value of variable, pointer is dereferenced, using the indirection operator *.
  • 10. Points to remember:  While declaring/initializing the pointer variable, * indicates that the variable is a pointer.  The address of any variable is given by preceding the variable name with Ampersand '&'.  The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value.  To access the value of a certain address stored by a pointer variable, * is used. Here, the * can be read as 'value at'.
  • 11. A Simple program to explain pointers:
  • 12. Pointer Arithmetic  16 bit Machine ( Turbo C ) In a 16 bit machine, size of all types of pointer, be it int*, float*, char* or double* is always 2 bytes. But when we perform any arithmetic function like increment on a pointer, changes occur as per the size of their primitive data type.
  • 13.
  • 14. Examples for Pointer Arithmetic In the above case, pointer will be of 2 bytes. And when we increment it, it will increment by 2 bytes because int is also of 2 bytes. In this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 4 bytes because float is of 4 bytes. Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 8 bytes because its data type is double.
  • 15. 32 bit Machine (Visual Basic C++)  The concept of pointer arithmetic remains exact same, but the size of pointer and various datatypes is different in a 32 bit machine. Pointer in 32 bit machine is of 4 bytes.
  • 16.  And, following is a table for Size of datatypes on 32-bit Machine :  Note: We cannot add two pointers. This is because pointers contain addresses, adding two addresses makes no sense, because you have no idea what it would point to.But we can subtract two pointers.This is because difference between two pointers gives the number of elements of its data type that can be stored between the two pointers.
  • 18.  Explanation of the above program:  Point 1: Here, * means 'value at the given address'. Thus, it adds the value of m and n which is 15.  Point 2: It subtracts the addresses of the two variables and then divides it by the size of the pointer datatype (here integer, which has size of 4 bytes) which gives us the number of elements of integer data type that can be stored within it.  Point 3: It increments the address stored by the pointer by the size of its datatype(here 4).  Point 4: It decrements the address stored by the pointer by the size of its datatype(here 4).  Point 5: Addition of two pointers is not allowed.
  • 19. Pointer and 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.  Suppose we declare an array arr,
  • 20.  Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will be stored as follows:  Here variable arr will give the base address, which is a constant pointer pointing to the element, arr[0].  Therefore arr is containing the address of arr[0] i.e 1000.  In short, arr has two purpose - it is the name of an array and it acts as a pointer pointing towards the first element in the array. arr is equal to &arr[0] //by default
  • 21.  We can declare a pointer of type int to point to the array arr.  Now we can access every element of array arr using p++ to move from one element to another. NOTE : You cannot decrement a pointer once incremented. p-- won't work.
  • 22. Example void main() { int x[5]={2,4,6,8,10},i=0; clrscr(); printf(“nElement Number Element Address”); while(i<5) { printf(“nx[%d] = t%d %u”,i,*(x+i),x+i); i++; }
  • 23. Output Element Number Element Address X[0]= 2 4056 X[1]= 4 4058 X[2]= 6 4060 X[3]= 8 4062 X[4]= 10 4064
  • 24. Pointer to Array  we can use a pointer to point to an Array, and then we can use that pointer to access the array.  Example:  In the above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base address (a in above case) to act as pointer and print all the values.
  • 25.
  • 26.  The generalized form for using pointer with an array, is same as:
  • 27. Array of pointers Example, which uses an array of 3 integers − When the above code is compiled and executed, it produces the following result −
  • 28.  There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer −  It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer to an int value.
  • 29.  The following example uses three integers, which are stored in an array of pointers, as follows − When the above code is compiled and executed, it produces the following result −
  • 30. Pointers to Function  It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows,
  • 31.  A function pointer can point to a specific function when it is assigned the name of the function.  s is a pointer to a function sum. Now sum can be called using function pointer s with the list of parameter.
  • 32. Example of Pointer to Function