SlideShare a Scribd company logo
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 Python
Raajendra M
 
Types of function call
Types of function callTypes of function call
Types of function call
ArijitDhali
 
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 c
Saket Pathak
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
C function
C functionC function
C function
thirumalaikumar3
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
C function presentation
C function presentationC function presentation
C function presentation
Touhidul Shawan
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Functions in C
Functions in CFunctions in C
Functions in C
Princy 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 C
Syed Mustafa
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
Vaishnavee Sharma
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Lakshmi Sarvani Videla
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd 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

C questions
C questionsC questions
C questions
mohamed sikander
 
Function basics
Function basicsFunction basics
Function basics
mohamed sikander
 
Cquestions
Cquestions Cquestions
Cquestions
mohamed sikander
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
Arrays
ArraysArrays
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
LeahRachael
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
C basics
C basicsC basics
C basicsMSc CST
 
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
vrgokila
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
Export Promotion Bureau
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
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
KavyaSharma65
 

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

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Mohammed Sikander
 
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
Mohammed Sikander
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
Mohammed Sikander
 
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
Mohammed Sikander
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
Mohammed Sikander
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Python set
Python setPython set
Python set
Mohammed Sikander
 
Python list
Python listPython list
Python list
Mohammed Sikander
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
Pipe
PipePipe
Signal
SignalSignal
File management
File managementFile management
File management
Mohammed Sikander
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 

More from Mohammed Sikander (20)

Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
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
 
Pipe
PipePipe
Pipe
 
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
 

Recently uploaded

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 

Recently uploaded (20)

PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 

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