SlideShare a Scribd company logo
1 of 16
SSASIT - Surat
Computer Programming & Utilization
Pointers
Prepared By:
Lalji Shiyani
Students of ssasit
1
Introduction
Pointers
Pointers are the variable that contains
the address of another variable within
memory.
2
Pointer Variable Declarations and
Initialization
Pointer variables
 Contain memory addresses as their values
 Normal variables contain a specific value (direct
reference)
 Pointers contain address of a variable that has a
specific value (indirect reference)
 Indirection – referencing a pointer value
 
count
7
count
7
countPtr
3
Pointer Variable Declarations and
Initialization
 Pointer declarations
 * used with pointer variables
int *myPtr;
 Declares a pointer to an int (pointer of type int *)
 Multiple pointers require using a * before each variable
declaration
int *myPtr1, *myPtr2;
 Can declare pointers to any data type
 Initialize pointers to 0, NULL, or an address
 0 or NULL – points to nothing (NULL preferred)
4
Need of Pointer
• It enhances the capability of the language in manipulating
the data.
• It reduces the size of the program.
• It is used for creating data structures such as linked lists,
trees, graphs etc.
• It is used to pass information between different functions
in both directions.
• It increases the execution speed of the program.
• If used with array of strings, then it reduces the storage
space
requirement.
• With pointers, we can use dynamic memory allocation.
5
Pointer Operators
& (address operator)
 Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets
address of y
yPtr
y
5
yptr
1000 1002
y
1002 5
Address of y
is value of
yptr
6
Pointer Operators
 * (indirection/dereferencing operator)
 Returns a synonym/alias of what its operand
points to
 *yptr returns y (because yptr points to y)
 * can be used for assignment
 Returns alias to an object
*yptr = 7; // changes y to 7
 Dereferenced pointer (operand of *) must be an
lvalue (no constants)
 * and & are inverses
 They cancel each other out
7
main()
{
int a;
int *aPtr;
a = 7;
aPtr = &a;
printf( "The address of a is %u n The value of aPtr is %u", &a, aPtr );
printf( "nThe value of a is %d n The value of *aPtr is %d", a, *aPtr );
printf(“ &*aPtr = %u" , &*aPtr);
}
The address of a is 0012FF88
The value of aPtr is 0012FF88
The value of a is 7
The value of *aPtr is 7
&*aPtr = 0012FF88
8
Calling Functions by Reference
 Call by reference with pointer arguments
 Pass address of argument using & operator
 Allows you to change actual location in memory
 Arrays are not passed with & because the array name is
already a pointer
 * operator
 Used as alias/nickname for variable inside of function
void double( int *number )
{
*number = 2 * ( *number );
}
 *number used as nickname for the variable passed9
© 2000 Prentice Hall, Inc.
All rights reserved.
1. Function prototype
1.1 Initialize variables
2. Call function
3. Define function
Program Output
1/* Fig. 7.7: fig07_07.c
2 Cube a variable using call-by-reference
3 with a pointer argument */
4
5#include <stdio.h>
6
7void cubeByReference( int * ); /* prototype */
8
9int main()
10 {
11 int number = 5;
12
13 printf( "The original value of number is %d", number );
14 cubeByReference( &number );
15 printf( "nThe new value of number is %dn", number );
16
17 return 0;
18 }
19
20 void cubeByReference( int *nPtr )
21 {
22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */
23 }
The original value of number is 5
The new value of number is 125
Notice how the address of
number is given -
cubeByReference expects a
pointer (an address of a variable).
Inside cubeByReference, *nPtr
is used (*nPtr is number).
Notice that the function prototype
takes a pointer to an integer (int *).
10
Pointer Expressions and Pointer
Arithmetic
Arithmetic operations can be
performed on pointers
 Increment/decrement pointer (++ or --)
 Add an integer to a pointer( + or += , - or -=)
 Pointers may be subtracted from each other
 Operations meaningless unless performed on an
array
11
Pointer Expressions and Pointer
Arithmetic5 element int array on machine with 4
byte ints
 vPtr points to first element v[ 0 ]
at location 3000 (vPtr = 3000)
 vPtr += 2; sets vPtr to 3008
vPtr points to v[ 2 ] (incremented by 2), but
the machine has 4 byte ints, so it points to
address 3008
pointer variable vPtr
v[0] v[1] v[2] v[4]v[3]
3000 3004 3008 3012 3016
location
12
Pointer Expressions and Pointer
Arithmetic
Subtracting pointers
 Returns number of elements from one to the
other. If
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
 vPtr2 - vPtr would produce 2
Pointer comparison ( <, == , > )
 See which pointer points to the higher numbered
array element
 Also, see if a pointer points to 0
13
The Relationship Between Pointers and
Arrays
 Arrays and pointers closely related
 Array name like a constant pointer
 Pointers can do array subscripting operations
 Declare an array b[ 5 ] and a pointer bPtr
 To set them equal to one another use:
bPtr = b;
The array name (b) is actually the address of first
element of the array b[ 5 ]
bPtr = &b[ 0 ]
Explicitly assigns bPtr to address of first element of b
14
The Relationship Between Pointers and
Arrays
 Element b[ 3 ]
Can be accessed by *( bPtr + 3 )
Where n is the offset. Called pointer/offset
notation
Can be accessed by bptr[ 3 ]
Called pointer/subscript notation
bPtr[ 3 ] same as b[ 3 ]
Can be accessed by performing pointer arithmetic
on the array itself
*( b + 3 )
15
Arrays of Pointers Arrays can contain pointers
 For example: an array of strings
char *suit[ 4 ] = { "Hearts", "Diamonds",
"Clubs", "Spades" };
 Strings are pointers to the first character
 char * – each element of suit is a pointer to a char
 The strings are not actually stored in the array suit, only pointers to the
strings are stored
 suit array has a fixed size, but strings can be of any size
suit[3]
suit[2]
suit[1]
suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’0’
’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’0’
’C’ ’l’ ’u’ ’b’ ’s’ ’0’
’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’0’
 
16

More Related Content

What's hot (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
pointers
pointerspointers
pointers
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
This pointer
This pointerThis pointer
This pointer
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 

Similar to Pointers

Lecture15.pdf
Lecture15.pdfLecture15.pdf
Lecture15.pdfJoyPalit
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
Pointer introduction day1
Pointer introduction day1Pointer introduction day1
Pointer introduction day1Bhuvana Gowtham
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfsudhakargeruganti
 
C Pointers
C PointersC Pointers
C Pointersomukhtar
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 

Similar to Pointers (20)

Pointer in C
Pointer in CPointer in C
Pointer in C
 
Lecture15.pdf
Lecture15.pdfLecture15.pdf
Lecture15.pdf
 
Pointers
PointersPointers
Pointers
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers Notes.pptx
Pointers Notes.pptxPointers Notes.pptx
Pointers Notes.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointer introduction day1
Pointer introduction day1Pointer introduction day1
Pointer introduction day1
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
C Pointers
C PointersC Pointers
C Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

Pointers

  • 1. SSASIT - Surat Computer Programming & Utilization Pointers Prepared By: Lalji Shiyani Students of ssasit 1
  • 2. Introduction Pointers Pointers are the variable that contains the address of another variable within memory. 2
  • 3. Pointer Variable Declarations and Initialization Pointer variables  Contain memory addresses as their values  Normal variables contain a specific value (direct reference)  Pointers contain address of a variable that has a specific value (indirect reference)  Indirection – referencing a pointer value   count 7 count 7 countPtr 3
  • 4. Pointer Variable Declarations and Initialization  Pointer declarations  * used with pointer variables int *myPtr;  Declares a pointer to an int (pointer of type int *)  Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2;  Can declare pointers to any data type  Initialize pointers to 0, NULL, or an address  0 or NULL – points to nothing (NULL preferred) 4
  • 5. Need of Pointer • It enhances the capability of the language in manipulating the data. • It reduces the size of the program. • It is used for creating data structures such as linked lists, trees, graphs etc. • It is used to pass information between different functions in both directions. • It increases the execution speed of the program. • If used with array of strings, then it reduces the storage space requirement. • With pointers, we can use dynamic memory allocation. 5
  • 6. Pointer Operators & (address operator)  Returns address of operand int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr y 5 yptr 1000 1002 y 1002 5 Address of y is value of yptr 6
  • 7. Pointer Operators  * (indirection/dereferencing operator)  Returns a synonym/alias of what its operand points to  *yptr returns y (because yptr points to y)  * can be used for assignment  Returns alias to an object *yptr = 7; // changes y to 7  Dereferenced pointer (operand of *) must be an lvalue (no constants)  * and & are inverses  They cancel each other out 7
  • 8. main() { int a; int *aPtr; a = 7; aPtr = &a; printf( "The address of a is %u n The value of aPtr is %u", &a, aPtr ); printf( "nThe value of a is %d n The value of *aPtr is %d", a, *aPtr ); printf(“ &*aPtr = %u" , &*aPtr); } The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 &*aPtr = 0012FF88 8
  • 9. Calling Functions by Reference  Call by reference with pointer arguments  Pass address of argument using & operator  Allows you to change actual location in memory  Arrays are not passed with & because the array name is already a pointer  * operator  Used as alias/nickname for variable inside of function void double( int *number ) { *number = 2 * ( *number ); }  *number used as nickname for the variable passed9
  • 10. © 2000 Prentice Hall, Inc. All rights reserved. 1. Function prototype 1.1 Initialize variables 2. Call function 3. Define function Program Output 1/* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference 3 with a pointer argument */ 4 5#include <stdio.h> 6 7void cubeByReference( int * ); /* prototype */ 8 9int main() 10 { 11 int number = 5; 12 13 printf( "The original value of number is %d", number ); 14 cubeByReference( &number ); 15 printf( "nThe new value of number is %dn", number ); 16 17 return 0; 18 } 19 20 void cubeByReference( int *nPtr ) 21 { 22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */ 23 } The original value of number is 5 The new value of number is 125 Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable). Inside cubeByReference, *nPtr is used (*nPtr is number). Notice that the function prototype takes a pointer to an integer (int *). 10
  • 11. Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers  Increment/decrement pointer (++ or --)  Add an integer to a pointer( + or += , - or -=)  Pointers may be subtracted from each other  Operations meaningless unless performed on an array 11
  • 12. Pointer Expressions and Pointer Arithmetic5 element int array on machine with 4 byte ints  vPtr points to first element v[ 0 ] at location 3000 (vPtr = 3000)  vPtr += 2; sets vPtr to 3008 vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to address 3008 pointer variable vPtr v[0] v[1] v[2] v[4]v[3] 3000 3004 3008 3012 3016 location 12
  • 13. Pointer Expressions and Pointer Arithmetic Subtracting pointers  Returns number of elements from one to the other. If vPtr2 = v[ 2 ]; vPtr = v[ 0 ];  vPtr2 - vPtr would produce 2 Pointer comparison ( <, == , > )  See which pointer points to the higher numbered array element  Also, see if a pointer points to 0 13
  • 14. The Relationship Between Pointers and Arrays  Arrays and pointers closely related  Array name like a constant pointer  Pointers can do array subscripting operations  Declare an array b[ 5 ] and a pointer bPtr  To set them equal to one another use: bPtr = b; The array name (b) is actually the address of first element of the array b[ 5 ] bPtr = &b[ 0 ] Explicitly assigns bPtr to address of first element of b 14
  • 15. The Relationship Between Pointers and Arrays  Element b[ 3 ] Can be accessed by *( bPtr + 3 ) Where n is the offset. Called pointer/offset notation Can be accessed by bptr[ 3 ] Called pointer/subscript notation bPtr[ 3 ] same as b[ 3 ] Can be accessed by performing pointer arithmetic on the array itself *( b + 3 ) 15
  • 16. Arrays of Pointers Arrays can contain pointers  For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };  Strings are pointers to the first character  char * – each element of suit is a pointer to a char  The strings are not actually stored in the array suit, only pointers to the strings are stored  suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’0’ ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’0’ ’C’ ’l’ ’u’ ’b’ ’s’ ’0’ ’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’0’   16