SlideShare a Scribd company logo
1 of 37
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software International Limited
int main( )
{
int x = 10;
printf(β€œx = %d β€œ , x); //Prints the value of x.
printf(β€œ&x = %p β€œ , &x);
}
mohammed.sikander@cranessoftware.com 2
& operator gets the address of variable.
%p is the correct format specifier to print address.
1. int main( )
2. {
3. int x = 10;
4. printf(β€œx = %d nβ€œ , x); //Prints the value of x.
5. printf(β€œ&x = %p nβ€œ , &x);
6. int *ptr; //Declaration of pointer
7. ptr = &x;
8. printf(β€œptr = %p n” , ptr);
9. printf(β€œ*ptr = %d n” , *ptr);
10. }
mohammed.sikander@cranessoftware.com 3
6. * is used to declare a pointer
9. * is used to get value at address (dereference)
1. int main( )
2. {
3. int x = 5, y = 8;
4. int *p1 , p2;
5. p1 = &x;
6. p2 = &y;
7. printf(β€œ *p1 = %d β€œ , *p1);
8. printf(β€œ *p2 = %d β€œ , *p2);
9. }
mohammed.sikander@cranessoftware.com 4
mohammed.sikander@cranessoftware.com 5
Pointer size is same irrespective of type.
Pointers are like shorcuts to files (in windows).
A size of shortcut to a 1MB file, 1GB file ,
200MB file is same
mohammed.sikander@cranessoftware.com 6
mohammed.sikander@cranessoftware.com 8
int main( )
{
int x;
printf(β€œ x = %d β€œ , x); //garbage value.
int *ptr;
printf(β€œ ptr = %p β€œ , ptr); //garbage address.
printf(β€œ *ptr = %d β€œ ,*ptr);
}
mohammed.sikander@cranessoftware.com 9
int main( )
{
int *ptr1; // unitialized pointer
int *ptr2 = NULL; // NULL pointer
if(ptr2 != NULL) // Can be safeguarded
printf(β€œ *ptr2 = %d n”, *ptr2);
if(ptr1 != NULL) // Cannot be safeguarded
printf(β€œ *ptr1 = %d n”, *ptr1);
}
mohammed.sikander@cranessoftware.com 10
void update(int x)
{
x = x + 5;
printf(β€œUpdate x = %d β€œ , x);
}
int main( )
{
int x = 2;
update( x );
printf(β€œMain x = %d β€œ , x);
}
mohammed.sikander@cranessoftware.com 11
void update(int *px)
{
*px = *px + 5;
printf(β€œUpdate *px = %d β€œ , *px);
}
int main( )
{
int x = 2;
update( &x );
printf(β€œMain x = %d β€œ , x);
}
mohammed.sikander@cranessoftware.com 12
mohammed.sikander@cranessoftware.com 13
mohammed.sikander@cranessoftware.com 14
a = 3 , b = 7
a = a + b; //10
b = a – b; //3
a = a – b; //7
mohammed.sikander@cranessoftware.com 15
a = 7 , b = 10
a = a * b; //70
b = a / b; //7
a = a / b; //10
a = 5 , b = 10
a = a ^ b; //
b = a ^ b; //5
a = a ^ b; //10
int main( )
{
int arr[ ] = {12,23,34,54};
printf(β€œ %p β€œ , &arr[0]);
printf(β€œ %p β€œ , arr);
}
mohammed.sikander@cranessoftware.com 16
Array name gives the base address (address of first
element of array )
int main( )
{
int arr[ ] = {12,23,34,54};
int *p1 = &arr[0];
int *p2 = arr;
printf(β€œ %d β€œ , *p1);
printf(β€œ %d β€œ , *p2);
}
mohammed.sikander@cranessoftware.com 17
The following arithmetic operations with pointers are legal:
β€’ add an integer to a pointer (+ and +=)
β€’ subtract an integer from a pointer(- and -=).
β€’ use a pointer as an operand to the ++ and -- operators.
β€’ subtract one pointer from another pointer, if they point
to objects of the same type.
β€’ compare two pointers
β€’ Operations meaningless unless performed on an array
β€’ All other arithmetic operations with pointers are illegal.
ο‚‘ Ptr = ptr + int
ο‚‘ When you add or subtract an integer to or from a
pointer, the compiler automatically scales the
integer to the pointer's type. In this way, the integer
always represents the number of objects to jump,
not the number of bytes.
ο‚‘ int x; // Assume address of x is 1000
ο‚‘ int *ptr = &x; // ptr = 1000
ο‚‘ ptr = ptr +1; // ptr-> 1004 and not 1001
int main( )
{
int arr[ ] = {0x21343782 , 0x54562319};
int *p1 = &arr[0];
printf(β€œ &arr[0] = %p n” , &arr[0]); //100
printf(β€œ &arr[1] = %p n” , &arr[1]); //104
printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //100
p1++;
printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //104
}
mohammed.sikander@cranessoftware.com 20
int main( )
{
short int arr[ ] = {0x2134 , 0x5456};
short int *p1 = &arr[0];
printf(β€œ &arr[0] = %p n” , &arr[0]); //100
printf(β€œ &arr[1] = %p n” , &arr[1]); //102
printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //100
p1++;
printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //102
}
mohammed.sikander@cranessoftware.com 21
int main( )
{
int arr[ ] = {0x21343782 , 0x54562319};
short int *p1 = &arr[0];
printf(β€œ *p1 = %x n” ,*p1);
p1++;
printf(β€œ *p1 = %x n” ,*p1);
}
mohammed.sikander@cranessoftware.com 22
int main( )
{
int arr[ 5 ] = {12,23,34,45,56};
int *ptr = arr;
for(int i = 0 ; i < 5 ; i++)
{
printf(β€œ %d β€œ , *ptr);
ptr++;
}
}
mohammed.sikander@cranessoftware.com 23
int main( )
{
int arr[ 5 ] = {12,23,34,45,56};
int *ptr = arr;
for(int i = 0 ; i < 5 ; i++)
{
printf(β€œ %d β€œ , ptr[ i ]);
}
}
mohammed.sikander@cranessoftware.com 24
int main( )
{
int arr[ 5 ] = {12,23,34,45,56};
for(int i = 0 ; i < 5 ; i++)
{
printf(β€œ %d β€œ , *(arr + i));
}
}
mohammed.sikander@cranessoftware.com 25
void printArray(int arr[])
{
printf(" %d " , sizeof(arr));
}
int main( )
{
int arr[5] = {12,23,34,54,67};
printf(" %d " , sizeof(arr));
printArray( arr );
}
mohammed.sikander@cranessoftware.com 26
mohammed.sikander@cranessoftware.com 27
void printArray(int arr[])
{
arr++; //No Error
}
int main( )
{
int arr[5] = {12,23,34,54,67};
// arr++; //Error
printArray( arr );
}
mohammed.sikander@cranessoftware.com 28
int main( )
{
const int x= 5;
x = 20; //ERROR
x++; //ERROR
}
mohammed.sikander@cranessoftware.com 29
int main( )
{
1. const int x= 5;
2. int *ptr = &x;
3. *ptr = 20;
4. printf(β€œ %d β€œ , x);
}
mohammed.sikander@cranessoftware.com 30
int main( )
{
1. const int x= 5;
2. const int *ptr = &x;
3. *ptr = 20;
4. printf(β€œ %d β€œ , x);
}
ο‚‘ Constant Pointer : Pointer is fixed to one
location.
ο‚‘ Pointer to const : Pointer has read-only
access.
mohammed.sikander@cranessoftware.com 31
ο‚‘ int * const cp;
ο‚‘ const int * pc;
ο‚‘ Constant pointer should be initialized.
int main( )
{
int x = 10 ;
const int y = 20;
const int * pc1 ;
pc1 = &x;
pc1 = &y;
}
mohammed.sikander@cranessoftware.com 32
int main( )
{
int x = 10 ;
const int y = 20;
int * const cp1;
int * const cp2 = &x;
int * const cp3 = &y;
const int * const cp4 = &y;
cp2 = &y;
}
ο‚‘ size_t strlen(const char *str);
mohammed.sikander@cranessoftware.com 33
size_t mystrlen(const char *str)
{
const char *end = str;
while( *end != β€˜0’)
end++;
return end – str;
}
char * strcpy(char *dest, const char *src);
mohammed.sikander@cranessoftware.com 34
void mystrcpy(char *dest, const char *src)
{
while(1)
{
*dest = *src;
if(*dest == β€˜0’)
return;
src++;
dest++;
}
}
char * strcpy(char *dest, const char *src);
mohammed.sikander@cranessoftware.com 35
void mystrcpy(char *dest, const char *src)
{
while((*dest++ = *src++))
{
}
}
int strcmp(const char *str1 , const char *str2);
mohammed.sikander@cranessoftware.com 36
int mystrcmp(const char *str1 , const char *str2)
{
while(1)
{
if(*str1 != *str2)
return *str1 - *str2;
if(*str1 == β€˜0’)
return 0;
str1++;
str2++;
}
}
while(*str1 == *str2 && *str1 != β€˜0’)
{
str1++;
str2++;
}
return *str1 - *str2;
mohammed.sikander@cranessoftware.com 37

More Related Content

What's hot

Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonRaajendra M
Β 
Types of function call
Types of function callTypes of function call
Types of function callArijitDhali
Β 
Function in C Language
Function in C Language Function in C Language
Function in C Language programmings guru
Β 
Recursion in c
Recursion in cRecursion in c
Recursion in cSaket Pathak
Β 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1Abu Bakr Ramadan
Β 
Function in c
Function in cFunction in c
Function in cRaj Tandukar
Β 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
Β 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
Β 
C function presentation
C function presentationC function presentation
C function presentationTouhidul Shawan
Β 
Operators in python
Operators in pythonOperators in python
Operators in pythonPrabhakaran V M
Β 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
Β 
Functions in C
Functions in CFunctions in C
Functions in CPrincy Nelson
Β 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
Β 
C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
Β 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & ImportMohd Sajjad
Β 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)Rupendra Choudhary
Β 

What's hot (20)

Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
Β 
Types of function call
Types of function callTypes of function call
Types of function call
Β 
Function in C program
Function in C programFunction in C program
Function in C program
Β 
Function in C Language
Function in C Language Function in C Language
Function in C Language
Β 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Β 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Β 
Function in c
Function in cFunction in c
Function in c
Β 
Functions in c language
Functions in c language Functions in c language
Functions in c language
Β 
C function
C functionC function
C function
Β 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
Β 
C function presentation
C function presentationC function presentation
C function presentation
Β 
Operators in python
Operators in pythonOperators in python
Operators in python
Β 
Function in c program
Function in c programFunction in c program
Function in c program
Β 
Functions in C
Functions in CFunctions in C
Functions in C
Β 
Functions in C
Functions in CFunctions in C
Functions in C
Β 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Β 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
Β 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Β 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Β 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
Β 

Similar to Pointer basics

Similar to Pointer basics (20)

C questions
C questionsC questions
C questions
Β 
Function basics
Function basicsFunction basics
Function basics
Β 
Cquestions
Cquestions Cquestions
Cquestions
Β 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
Β 
C lab programs
C lab programsC lab programs
C lab programs
Β 
C lab programs
C lab programsC lab programs
C lab programs
Β 
Arrays
ArraysArrays
Arrays
Β 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Β 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Β 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Β 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
Β 
Functions
FunctionsFunctions
Functions
Β 
C basics
C basicsC basics
C basics
Β 
Tu1
Tu1Tu1
Tu1
Β 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
Β 
9.C Programming
9.C Programming9.C Programming
9.C Programming
Β 
week-5x
week-5xweek-5x
week-5x
Β 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
Β 
7 functions
7  functions7  functions
7 functions
Β 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
Β 

More from Mohammed Sikander

More from Mohammed Sikander (20)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
Β 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
Β 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
Β 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
Β 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Β 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Β 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Β 
Python tuple
Python   tuplePython   tuple
Python tuple
Β 
Python strings
Python stringsPython strings
Python strings
Β 
Python set
Python setPython set
Python set
Β 
Python list
Python listPython list
Python list
Β 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Β 
Signal
SignalSignal
Signal
Β 
File management
File managementFile management
File management
Β 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Β 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Β 
Java arrays
Java    arraysJava    arrays
Java arrays
Β 
Java strings
Java   stringsJava   strings
Java strings
Β 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
Β 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
Β 

Recently uploaded

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
Β 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
Β 
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
Β 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
Β 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
Β 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
Β 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
Β 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
Β 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
Β 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
Β 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
Β 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
Β 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
Β 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
Β 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
Β 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
Β 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
Β 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
Β 

Recently uploaded (20)

AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
Β 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Β 
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
β€œOh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Β 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
Β 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
Β 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
Β 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
Β 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Β 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
Β 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
Β 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
Β 
Model Call Girl in Bikash Puri Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Bikash Puri  Delhi reach out to us at πŸ”9953056974πŸ”Model Call Girl in Bikash Puri  Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Bikash Puri Delhi reach out to us at πŸ”9953056974πŸ”
Β 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
Β 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
Β 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
Β 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
Β 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
Β 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
Β 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
Β 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
Β 

Pointer basics

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited
  • 2. int main( ) { int x = 10; printf(β€œx = %d β€œ , x); //Prints the value of x. printf(β€œ&x = %p β€œ , &x); } mohammed.sikander@cranessoftware.com 2 & operator gets the address of variable. %p is the correct format specifier to print address.
  • 3. 1. int main( ) 2. { 3. int x = 10; 4. printf(β€œx = %d nβ€œ , x); //Prints the value of x. 5. printf(β€œ&x = %p nβ€œ , &x); 6. int *ptr; //Declaration of pointer 7. ptr = &x; 8. printf(β€œptr = %p n” , ptr); 9. printf(β€œ*ptr = %d n” , *ptr); 10. } mohammed.sikander@cranessoftware.com 3 6. * is used to declare a pointer 9. * is used to get value at address (dereference)
  • 4. 1. int main( ) 2. { 3. int x = 5, y = 8; 4. int *p1 , p2; 5. p1 = &x; 6. p2 = &y; 7. printf(β€œ *p1 = %d β€œ , *p1); 8. printf(β€œ *p2 = %d β€œ , *p2); 9. } mohammed.sikander@cranessoftware.com 4
  • 6. Pointer size is same irrespective of type. Pointers are like shorcuts to files (in windows). A size of shortcut to a 1MB file, 1GB file , 200MB file is same mohammed.sikander@cranessoftware.com 6
  • 7.
  • 9. int main( ) { int x; printf(β€œ x = %d β€œ , x); //garbage value. int *ptr; printf(β€œ ptr = %p β€œ , ptr); //garbage address. printf(β€œ *ptr = %d β€œ ,*ptr); } mohammed.sikander@cranessoftware.com 9
  • 10. int main( ) { int *ptr1; // unitialized pointer int *ptr2 = NULL; // NULL pointer if(ptr2 != NULL) // Can be safeguarded printf(β€œ *ptr2 = %d n”, *ptr2); if(ptr1 != NULL) // Cannot be safeguarded printf(β€œ *ptr1 = %d n”, *ptr1); } mohammed.sikander@cranessoftware.com 10
  • 11. void update(int x) { x = x + 5; printf(β€œUpdate x = %d β€œ , x); } int main( ) { int x = 2; update( x ); printf(β€œMain x = %d β€œ , x); } mohammed.sikander@cranessoftware.com 11
  • 12. void update(int *px) { *px = *px + 5; printf(β€œUpdate *px = %d β€œ , *px); } int main( ) { int x = 2; update( &x ); printf(β€œMain x = %d β€œ , x); } mohammed.sikander@cranessoftware.com 12
  • 15. a = 3 , b = 7 a = a + b; //10 b = a – b; //3 a = a – b; //7 mohammed.sikander@cranessoftware.com 15 a = 7 , b = 10 a = a * b; //70 b = a / b; //7 a = a / b; //10 a = 5 , b = 10 a = a ^ b; // b = a ^ b; //5 a = a ^ b; //10
  • 16. int main( ) { int arr[ ] = {12,23,34,54}; printf(β€œ %p β€œ , &arr[0]); printf(β€œ %p β€œ , arr); } mohammed.sikander@cranessoftware.com 16 Array name gives the base address (address of first element of array )
  • 17. int main( ) { int arr[ ] = {12,23,34,54}; int *p1 = &arr[0]; int *p2 = arr; printf(β€œ %d β€œ , *p1); printf(β€œ %d β€œ , *p2); } mohammed.sikander@cranessoftware.com 17
  • 18. The following arithmetic operations with pointers are legal: β€’ add an integer to a pointer (+ and +=) β€’ subtract an integer from a pointer(- and -=). β€’ use a pointer as an operand to the ++ and -- operators. β€’ subtract one pointer from another pointer, if they point to objects of the same type. β€’ compare two pointers β€’ Operations meaningless unless performed on an array β€’ All other arithmetic operations with pointers are illegal.
  • 19. ο‚‘ Ptr = ptr + int ο‚‘ When you add or subtract an integer to or from a pointer, the compiler automatically scales the integer to the pointer's type. In this way, the integer always represents the number of objects to jump, not the number of bytes. ο‚‘ int x; // Assume address of x is 1000 ο‚‘ int *ptr = &x; // ptr = 1000 ο‚‘ ptr = ptr +1; // ptr-> 1004 and not 1001
  • 20. int main( ) { int arr[ ] = {0x21343782 , 0x54562319}; int *p1 = &arr[0]; printf(β€œ &arr[0] = %p n” , &arr[0]); //100 printf(β€œ &arr[1] = %p n” , &arr[1]); //104 printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //100 p1++; printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //104 } mohammed.sikander@cranessoftware.com 20
  • 21. int main( ) { short int arr[ ] = {0x2134 , 0x5456}; short int *p1 = &arr[0]; printf(β€œ &arr[0] = %p n” , &arr[0]); //100 printf(β€œ &arr[1] = %p n” , &arr[1]); //102 printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //100 p1++; printf(β€œ p1 = %p *p1 = %x” , p1 , *p1); //102 } mohammed.sikander@cranessoftware.com 21
  • 22. int main( ) { int arr[ ] = {0x21343782 , 0x54562319}; short int *p1 = &arr[0]; printf(β€œ *p1 = %x n” ,*p1); p1++; printf(β€œ *p1 = %x n” ,*p1); } mohammed.sikander@cranessoftware.com 22
  • 23. int main( ) { int arr[ 5 ] = {12,23,34,45,56}; int *ptr = arr; for(int i = 0 ; i < 5 ; i++) { printf(β€œ %d β€œ , *ptr); ptr++; } } mohammed.sikander@cranessoftware.com 23
  • 24. int main( ) { int arr[ 5 ] = {12,23,34,45,56}; int *ptr = arr; for(int i = 0 ; i < 5 ; i++) { printf(β€œ %d β€œ , ptr[ i ]); } } mohammed.sikander@cranessoftware.com 24
  • 25. int main( ) { int arr[ 5 ] = {12,23,34,45,56}; for(int i = 0 ; i < 5 ; i++) { printf(β€œ %d β€œ , *(arr + i)); } } mohammed.sikander@cranessoftware.com 25
  • 26. void printArray(int arr[]) { printf(" %d " , sizeof(arr)); } int main( ) { int arr[5] = {12,23,34,54,67}; printf(" %d " , sizeof(arr)); printArray( arr ); } mohammed.sikander@cranessoftware.com 26
  • 27. mohammed.sikander@cranessoftware.com 27 void printArray(int arr[]) { arr++; //No Error } int main( ) { int arr[5] = {12,23,34,54,67}; // arr++; //Error printArray( arr ); }
  • 29. int main( ) { const int x= 5; x = 20; //ERROR x++; //ERROR } mohammed.sikander@cranessoftware.com 29 int main( ) { 1. const int x= 5; 2. int *ptr = &x; 3. *ptr = 20; 4. printf(β€œ %d β€œ , x); }
  • 30. mohammed.sikander@cranessoftware.com 30 int main( ) { 1. const int x= 5; 2. const int *ptr = &x; 3. *ptr = 20; 4. printf(β€œ %d β€œ , x); }
  • 31. ο‚‘ Constant Pointer : Pointer is fixed to one location. ο‚‘ Pointer to const : Pointer has read-only access. mohammed.sikander@cranessoftware.com 31 ο‚‘ int * const cp; ο‚‘ const int * pc; ο‚‘ Constant pointer should be initialized.
  • 32. int main( ) { int x = 10 ; const int y = 20; const int * pc1 ; pc1 = &x; pc1 = &y; } mohammed.sikander@cranessoftware.com 32 int main( ) { int x = 10 ; const int y = 20; int * const cp1; int * const cp2 = &x; int * const cp3 = &y; const int * const cp4 = &y; cp2 = &y; }
  • 33. ο‚‘ size_t strlen(const char *str); mohammed.sikander@cranessoftware.com 33 size_t mystrlen(const char *str) { const char *end = str; while( *end != β€˜0’) end++; return end – str; }
  • 34. char * strcpy(char *dest, const char *src); mohammed.sikander@cranessoftware.com 34 void mystrcpy(char *dest, const char *src) { while(1) { *dest = *src; if(*dest == β€˜0’) return; src++; dest++; } }
  • 35. char * strcpy(char *dest, const char *src); mohammed.sikander@cranessoftware.com 35 void mystrcpy(char *dest, const char *src) { while((*dest++ = *src++)) { } }
  • 36. int strcmp(const char *str1 , const char *str2); mohammed.sikander@cranessoftware.com 36 int mystrcmp(const char *str1 , const char *str2) { while(1) { if(*str1 != *str2) return *str1 - *str2; if(*str1 == β€˜0’) return 0; str1++; str2++; } } while(*str1 == *str2 && *str1 != β€˜0’) { str1++; str2++; } return *str1 - *str2;

Editor's Notes

  1. #include <stdio.h> int main( ) { char c , *pc; short int si , *psi; int i , *pi; double d , *pd; printf(" c %d pc %d \n" , sizeof(c) , sizeof(pc)); printf(" si %d psi %d \n" , sizeof(si) , sizeof(psi)); printf(" i %d pi %d \n" , sizeof(i) , sizeof(pi)); printf(" d %d pd %d \n" , sizeof(d) , sizeof(pd)); return 0; }
  2. Use typecasting to remove warnings
  3. #include <stdio.h> void swap( int *pa , int *pb) { int temp = *pa; *pa = *pb; *pb = temp; printf(" *pa = %d *pb = %d \n" ,*pa , *pb); } int main( ) { int a = 5 , b = 10; swap( &a , &b ); printf("a = %d b = %d \n" , a , b); }
  4. #include <stdio.h> void swap( int *pa , int *pb) { int *temp = pa; pa = pb; pb = temp; printf(" *pa = %d *pb = %d \n" ,*pa , *pb); } int main( ) { int a = 5 , b = 10; swap( &a , &b ); printf("a = %d b = %d \n" , a , b); }
  5. b = (a + b) - (a = b) // Karthik - 110