SlideShare a Scribd company logo
1 of 44
1
Pointers in C
2
Computer Memory Revisited
• Computers store data in memory slots
• Each slot has an unique address
• Variables store their values like this:
Addr Content Addr Content Addr Content Addr Content
1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘0’
1008 ptr: 1001 1009 … 1010 1011
3
Computer Memory Revisited
• Altering the value of a variable is indeed
changing the content of the memory
– e.g. i = 40; a[2] = ‘z’;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘0’
1008 ptr: 1001 1009 … 1010 1011
4
Addressing Concept
• Pointer stores the address of another entity
• It refers to a memory location
int i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */
5
What actually ptr is?
• ptr is a variable storing an address
• ptr is NOT storing the actual value of i
int i = 5;
int *ptr;
ptr = &i;
printf(“i = %dn”, i);
printf(“*ptr = %dn”, *ptr);
printf(“ptr = %pn”, ptr);
5i
address of iptr
Output:
i = 5
*ptr = 5
ptr = effff5e0
value of ptr =
address of i
in memory
6
Twin Operators
• &: Address-of operator
– Get the address of an entity
• e.g. ptr = &j;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74
1004 ptr: 1001 1005 1006 1007
7
Twin Operators
• *: De-reference operator
– Refer to the content of the referee
• e.g. *ptr = 99;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74
1004 ptr: 1001 1005 1006 1007
8
Example: Pass by Reference
• Modify behaviour in argument passing
void f(int j)
{
j = 5;
}
void g()
{
int i = 3;
f(i);
}
void f(int *ptr)
{
*ptr = 5;
}
void g()
{
int i = 3;
f(&i);
} i = ?i = ?i = 3 i = 5
9
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
10
An Illustration
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
11
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
pptr int ** integer pointer pointer variable
Double
Indirection
12
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 5
13
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
14
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 3
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 3
15
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
7
16
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 10
17
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
9
18
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
19
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable -2
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr -2
20
Pointer Arithmetic
• What’s ptr + 1?
The next memory location!
• What’s ptr - 1?
The previous memory location!
• What’s ptr * 2 and ptr / 2?
Invalid operations!!!
21
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable
*ptr float de-reference of float pointer
variable
?
22
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
?
23
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
24
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
?
25
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
9.0
26
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
?
27
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
6.0
28
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
29
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 7.0
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
7.0
30
Pointer Arithmetic and Array
• Type of a is float *
• a[2]  *(a + 2)
ptr = &(a[2])
ptr = &(*(a + 2))
ptr = a + 2
• a is a memory address constant
• ptr is a pointer variable
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
31
More Pointer Arithmetic
• What if a is a double array?
• A double may occupy more memory slots!
– Given double *ptr = a;
– What’s ptr + 1 then?
Addr Content Addr Content Addr Content Addr Content
1000 a[0]: 37.9 1001 … 1002 … 1003 …
1004 a[1]: 1.23 1005 … 1006 … 1007 …
1008 a[2]: 3.14 1009 … 1010 … 1011 …
32
More Pointer Arithmetic
• Arithmetic operators + and – auto-adjust the
address offset
• According to the type of the pointer:
– 1000 + sizeof(double) = 1000 + 4 = 1004
Addr Content Addr Content Addr Content Addr Content
1000 a[0]: 37.9 1001 … 1002 … 1003 …
1004 a[1]: 1.23 1005 … 1006 … 1007 …
1008 a[2]: 3.14 1009 … 1010 … 1011 …
Pointer and 2-D array
33
34
35
36
Array of Pointers
• We can define the array of pointers as
– datatype *name[index]
37
38
Void Pointer
• Special type of pointer that can point to any
data type.
• Type casting must be used to change void
pointer to concrete data type.
39
Demo Program
40
Passing arrays to function
• Array can be passed to function in two ways:
– Pass entire array
– Pass element by element
41
42
43
44

More Related Content

What's hot (20)

Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
File in c
File in cFile in c
File in c
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
functions of C++
functions of C++functions of C++
functions of C++
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C Pointers
C PointersC Pointers
C Pointers
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Enums in c
Enums in cEnums in c
Enums in c
 
Inline function
Inline functionInline function
Inline function
 

Viewers also liked

Viewers also liked (16)

Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Conceptualización sobre informatica
Conceptualización sobre informaticaConceptualización sobre informatica
Conceptualización sobre informatica
 
FUNDAMENTOS DE LA INFORMATICA - SESION 04
FUNDAMENTOS DE LA INFORMATICA - SESION 04FUNDAMENTOS DE LA INFORMATICA - SESION 04
FUNDAMENTOS DE LA INFORMATICA - SESION 04
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Pointers
PointersPointers
Pointers
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Network Layer
Network LayerNetwork Layer
Network Layer
 
Strings in C
Strings in CStrings in C
Strings in C
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Linked list
Linked listLinked list
Linked list
 
Array in C
Array in CArray in C
Array in C
 
Array in c language
Array in c languageArray in c language
Array in c language
 

Similar to Pointers in C

9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function웅식 전
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxhappycocoman
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 

Similar to Pointers in C (20)

CPointer-hk.ppt
CPointer-hk.pptCPointer-hk.ppt
CPointer-hk.ppt
 
C pointers
C pointersC pointers
C pointers
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Pointers Notes.pptx
Pointers Notes.pptxPointers Notes.pptx
Pointers Notes.pptx
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
Pointer
PointerPointer
Pointer
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
pointers
pointerspointers
pointers
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
c programming
c programmingc programming
c programming
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 

More from Kamal Acharya

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer SecurityKamal Acharya
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHPKamal Acharya
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in phpKamal Acharya
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data WarehousingKamal Acharya
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data MiningKamal Acharya
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data miningKamal Acharya
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingKamal Acharya
 

More from Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Recently uploaded

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
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
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
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
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
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
 
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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine 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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 

Recently uploaded (20)

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
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
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
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
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.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
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 

Pointers in C

  • 2. 2 Computer Memory Revisited • Computers store data in memory slots • Each slot has an unique address • Variables store their values like this: Addr Content Addr Content Addr Content Addr Content 1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘0’ 1008 ptr: 1001 1009 … 1010 1011
  • 3. 3 Computer Memory Revisited • Altering the value of a variable is indeed changing the content of the memory – e.g. i = 40; a[2] = ‘z’; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘0’ 1008 ptr: 1001 1009 … 1010 1011
  • 4. 4 Addressing Concept • Pointer stores the address of another entity • It refers to a memory location int i = 5; int *ptr; /* declare a pointer variable */ ptr = &i; /* store address-of i to ptr */ printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */
  • 5. 5 What actually ptr is? • ptr is a variable storing an address • ptr is NOT storing the actual value of i int i = 5; int *ptr; ptr = &i; printf(“i = %dn”, i); printf(“*ptr = %dn”, *ptr); printf(“ptr = %pn”, ptr); 5i address of iptr Output: i = 5 *ptr = 5 ptr = effff5e0 value of ptr = address of i in memory
  • 6. 6 Twin Operators • &: Address-of operator – Get the address of an entity • e.g. ptr = &j; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
  • 7. 7 Twin Operators • *: De-reference operator – Refer to the content of the referee • e.g. *ptr = 99; Addr Content Addr Content Addr Content Addr Content 1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74 1004 ptr: 1001 1005 1006 1007
  • 8. 8 Example: Pass by Reference • Modify behaviour in argument passing void f(int j) { j = 5; } void g() { int i = 3; f(i); } void f(int *ptr) { *ptr = 5; } void g() { int i = 3; f(&i); } i = ?i = ?i = 3 i = 5
  • 9. 9 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10
  • 10. 10 An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable
  • 11. 11 An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable pptr int ** integer pointer pointer variable Double Indirection
  • 12. 12 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable *ptr int de-reference of ptr 5
  • 13. 13 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i)
  • 14. 14 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 3 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 3
  • 15. 15 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr **pptr int de-reference of de-reference of pptr 7
  • 16. 16 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 10
  • 17. 17 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr **pptr int de-reference of de-reference of pptr 9
  • 18. 18 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i)
  • 19. 19 An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable -2 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr -2
  • 20. 20 Pointer Arithmetic • What’s ptr + 1? The next memory location! • What’s ptr - 1? The previous memory location! • What’s ptr * 2 and ptr / 2? Invalid operations!!!
  • 21. 21 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) ? a[3] float float array element (variable) ? ptr float * float pointer variable *ptr float de-reference of float pointer variable ?
  • 22. 22 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) ? a[3] float float array element (variable) ? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable ?
  • 23. 23 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) ? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14
  • 24. 24 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) ? ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable ?
  • 25. 25 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable 9.0
  • 26. 26 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable ?
  • 27. 27 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable 6.0
  • 28. 28 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14
  • 29. 29 Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 7.0
  • 30. 30 Pointer Arithmetic and Array • Type of a is float * • a[2]  *(a + 2) ptr = &(a[2]) ptr = &(*(a + 2)) ptr = a + 2 • a is a memory address constant • ptr is a pointer variable float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0;
  • 31. 31 More Pointer Arithmetic • What if a is a double array? • A double may occupy more memory slots! – Given double *ptr = a; – What’s ptr + 1 then? Addr Content Addr Content Addr Content Addr Content 1000 a[0]: 37.9 1001 … 1002 … 1003 … 1004 a[1]: 1.23 1005 … 1006 … 1007 … 1008 a[2]: 3.14 1009 … 1010 … 1011 …
  • 32. 32 More Pointer Arithmetic • Arithmetic operators + and – auto-adjust the address offset • According to the type of the pointer: – 1000 + sizeof(double) = 1000 + 4 = 1004 Addr Content Addr Content Addr Content Addr Content 1000 a[0]: 37.9 1001 … 1002 … 1003 … 1004 a[1]: 1.23 1005 … 1006 … 1007 … 1008 a[2]: 3.14 1009 … 1010 … 1011 …
  • 33. Pointer and 2-D array 33
  • 34. 34
  • 35. 35
  • 36. 36
  • 37. Array of Pointers • We can define the array of pointers as – datatype *name[index] 37
  • 38. 38
  • 39. Void Pointer • Special type of pointer that can point to any data type. • Type casting must be used to change void pointer to concrete data type. 39
  • 41. Passing arrays to function • Array can be passed to function in two ways: – Pass entire array – Pass element by element 41
  • 42. 42
  • 43. 43
  • 44. 44