SlideShare a Scribd company logo
1 of 56
1
Introduction
• Pointer is a variable that stores/points to the address of another variable.
• It is a derived data type in C.
• It is a built from the fundamental data types.
• A variable name directly references a value.
• A pointer variable must be declared before it can be used.
• Each memory cell in the computer has an address that can be used to access
that location so a pointer variable points to a memory location we can access
and change the contents of this memory location via the pointer.
2
Introduction
• Pointer is a variable that stores/points to the address of another variable.
• C pointer is used to allocate memory dynamically i.e. at runtime.
• i is the name given for particular memory location of ordinary variable.
• Let us consider, its corresponding address be 65524 and the value stored in variable
‘i’ is 3.
• The address of the variable ‘i’ is stored in another integer variable whose name is ‘j’
and which is having corresponding address 65522.
• Reference pointer (&) : If var is a variable then, &var is the address in memory.
• j = &i i.e. j = Address of i
3
Introduction
• Declaration of pointer
• Dereference pointer(*) are used for defining pointer variable.
• data_type* pointer_variable_name;
• int* p;  p as pointer variable of type int.
• & symbol is used to get the address of the variable.
• * symbol is used to get the value of the variable that the pointer is pointing to. This is
known as indirection operator or dereferencing operator. It is a unary operator.
4
5
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
6
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
7
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 */
8
Why do we need Pointer?
• Simply because it’s there!
• It is used in some circumstances in C
Remember this?
scanf(“%d”, &i);
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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)
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 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
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 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
20
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
21
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
22
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)
23
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
Pointer Declaration
• Pointer Declaration Style:
• int* p; /*Style 1*/
• int *p; /*Style 2*/
• int * p; /*Style 3*/
• Style 2 is popular because, convenient to have multiple declaration in the same
statement. Like,
• int *p, x, *q;
• This style matches with the format used for accessing the target values.
• For example,
• int x, *p, y;
• x=10;
• p = &x;
• y= *p;
• *p= 20; 24
Initialization of pointer variables
• The Process of assigning the address of a variable to a pointer variable is known as
initialization.
• Way1: int quantity;
int *p;
p=&quantity;
• Way 2: int quantity;
int *p=&quantity;
• Way 3: int x, *p=&x; (valid)
int *p=&x, x; (invalid)
• Way 4: int *p = NULL;
25
Initialization of pointer variables
• Pointer flexibility:
• We can make the same pointer to point to different data variables in
different statements.
• For example,
int x, y, z, *p;
………….
p=&x;
………….
p=&y;
………….
p=&z;
………….
26
Initialization of pointer variables
• Pointer flexibility:
• We can also use different pointers to point to same data variable.
• For example,
int x;
int *p1 = &x;
int *p2 = &x;
int *p3 = &x;
• The two statements,
• p=&q;
• n=*p;
• are equivalent to
• n=*&q;
• which in turn is equivalent to
• n=q; 27
Pointer Arithmetic
28
29
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!!!
30
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
?
31
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
?
32
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
33
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
?
34
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
35
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
?
36
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
37
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
38
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
39
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;
Pointer Increment
40
Pointer Increment
41
void pointer
• Suppose we have to declare integer pointer, character pointer and float pointer, then we
need to declare 3 pointer variables.
• Instead of declaring different types of pointer variable it is feasible to declare single
pointer variable which can act as integer pointer, character pointer.
• In C, General Purpose Pointer is called as void Pointer.
• It does not have any data type associated with it.
• It can store address of any type of variable.
• A void pointer is a C convention for a raw address.
• The compiler has no idea what type of object a void Pointer really points to?
• Declaration of void pointer : void *pointer_name;
• Why use void pointer ?  Reusability of pointers
42
void pointer
• void *ptr; // ptr is declared asVoid pointer
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum; // ptr has address of float data
• We have declared 3 variables of integer, character and float type.
• When we assign address of integer to the void pointer, pointer will become
Integer Pointer.
• When we assign address of Character Data type to void pointer it will become
Character Pointer.
• Similarly we can assign address of any data type to the void pointer.
• It is capable of storing address of any data type
43
Pointers and Arrays
• int arr[4];
• Name of the array is actually a pointer that points to the first element of the
array.
• Here, address of first element of an array is &arr[0].
• Also, arr represents the address of the pointer where it is pointing. Hence,
&arr[0] is equivalent to arr.
• Also, value inside the address &arr[0] and address arr are equal.
• Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0]
is equivalent to *arr.
44
Pointers and Arrays
• Similarly,
• &a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
• &a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
• &a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
• .
• &a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).
45
Pointers and Arrays
46
Pointers and Arrays
47
Pointers and Arrays
48
Pointers and Arrays
• Program for accessing array elements and
printing array elements in reverse using
pointer.
49
Some programs on arrays using pointer
• Program for accessing array elements and print array elements in reverse order
using pointer.
• Program for sorting n numbers in an array using pointer.
• Program for finding reverse of a string using pointer.
• Program to manipulate string using pointer (i.e find length, compare, concatenate
and copy string)
50
Pointers and functions – Call by
reference• When, argument is passed using pointer, address of the memory location is
passed instead of value.
• The address of memory location num1 and num2 are passed to function and
the pointers *a and *b accept those values. So, the pointer a and b points to
address of num1 and num2 respectively. When, the value of pointer are
changed, the value in memory location also changed correspondingly. Hence,
change made to *a and *b was reflected in num1 and num2 in main function.
51
Function returning a pointer
• Till now, we have seen functions that take arguments as pointers and returning
only the standard data type.
• We can have a function which does not return a value but a pointer to a value.
• The syntax is data_type *func_name(arguments);
• Here, * before the name of a function means that the functions returns a
pointer of the data_type mentioned. So, in the calling function the value must
be assigned to a pointer of the appropriate type.
52
Function returning a pointer
• In this case, must be careful because local variables of function does not live
outside the function, hence if you return a pointer connected to a local variable,
that pointer will be pointing to nothing when function ends.
53
Advantages of Pointers
1. Pointers are more efficient in handling arrays and data tables.
2. Pointers reduce length and complexity of programs.
3. They increase the execution speed and thus reduce the program execution time.
4. Pointers can be used to return multiple values from a function via function
arguments.
5. The use of pointer arrays to character strings results in saving of data storage
space in memory.
6. Pointer permit references to functions and thereby facilitating passing of
functions as arguments to other functions.
7. Pointers allow C to support dynamic memory management.
8. Pointers provide an efficient tool for manipulating dynamic structures such as
structures, linked lists, stacks.
54
Disadvantages of Pointers
1. Uninitialized pointers might cause segmentation fault.
2. Dynamically allocated block needs to be freed explicitly. Otherwise, it would
lead to memory leak.
3. Pointers are slower than normal variables.
4. If pointers are updated with incorrect values, it might lead to memory corruption
55
ANY QUESTIONS??
56

More Related Content

What's hot (20)

Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
pointers
pointerspointers
pointers
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
Data types in C
Data types in CData types in C
Data types in C
 
C pointer
C pointerC pointer
C pointer
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Strings in c
Strings in cStrings in c
Strings in c
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
C programming - String
C programming - StringC programming - String
C programming - String
 

Similar to Pointer Fundamentals Explained in 25 Steps

Similar to Pointer Fundamentals Explained in 25 Steps (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
C pointers
C pointersC pointers
C pointers
 
CPointer-hk.ppt
CPointer-hk.pptCPointer-hk.ppt
CPointer-hk.ppt
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointer
PointerPointer
Pointer
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Pointer
PointerPointer
Pointer
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers
PointersPointers
Pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 

Recently uploaded

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
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
 
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
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
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
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
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
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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)

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
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
 
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
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
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
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
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
 
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🔝
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.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 Fundamentals Explained in 25 Steps

  • 1. 1
  • 2. Introduction • Pointer is a variable that stores/points to the address of another variable. • It is a derived data type in C. • It is a built from the fundamental data types. • A variable name directly references a value. • A pointer variable must be declared before it can be used. • Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer. 2
  • 3. Introduction • Pointer is a variable that stores/points to the address of another variable. • C pointer is used to allocate memory dynamically i.e. at runtime. • i is the name given for particular memory location of ordinary variable. • Let us consider, its corresponding address be 65524 and the value stored in variable ‘i’ is 3. • The address of the variable ‘i’ is stored in another integer variable whose name is ‘j’ and which is having corresponding address 65522. • Reference pointer (&) : If var is a variable then, &var is the address in memory. • j = &i i.e. j = Address of i 3
  • 4. Introduction • Declaration of pointer • Dereference pointer(*) are used for defining pointer variable. • data_type* pointer_variable_name; • int* p;  p as pointer variable of type int. • & symbol is used to get the address of the variable. • * symbol is used to get the value of the variable that the pointer is pointing to. This is known as indirection operator or dereferencing operator. It is a unary operator. 4
  • 5. 5 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
  • 6. 6 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
  • 7. 7 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 */
  • 8. 8 Why do we need Pointer? • Simply because it’s there! • It is used in some circumstances in C Remember this? scanf(“%d”, &i);
  • 9. 9 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
  • 10. 10 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
  • 11. 11 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
  • 12. 12 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
  • 13. 13 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
  • 14. 14 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
  • 15. 15 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
  • 16. 16 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
  • 17. 17 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)
  • 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 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
  • 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 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
  • 20. 20 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
  • 21. 21 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
  • 22. 22 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)
  • 23. 23 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
  • 24. Pointer Declaration • Pointer Declaration Style: • int* p; /*Style 1*/ • int *p; /*Style 2*/ • int * p; /*Style 3*/ • Style 2 is popular because, convenient to have multiple declaration in the same statement. Like, • int *p, x, *q; • This style matches with the format used for accessing the target values. • For example, • int x, *p, y; • x=10; • p = &x; • y= *p; • *p= 20; 24
  • 25. Initialization of pointer variables • The Process of assigning the address of a variable to a pointer variable is known as initialization. • Way1: int quantity; int *p; p=&quantity; • Way 2: int quantity; int *p=&quantity; • Way 3: int x, *p=&x; (valid) int *p=&x, x; (invalid) • Way 4: int *p = NULL; 25
  • 26. Initialization of pointer variables • Pointer flexibility: • We can make the same pointer to point to different data variables in different statements. • For example, int x, y, z, *p; …………. p=&x; …………. p=&y; …………. p=&z; …………. 26
  • 27. Initialization of pointer variables • Pointer flexibility: • We can also use different pointers to point to same data variable. • For example, int x; int *p1 = &x; int *p2 = &x; int *p3 = &x; • The two statements, • p=&q; • n=*p; • are equivalent to • n=*&q; • which in turn is equivalent to • n=q; 27
  • 29. 29 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!!!
  • 30. 30 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 ?
  • 31. 31 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 ?
  • 32. 32 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
  • 33. 33 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 ?
  • 34. 34 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
  • 35. 35 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 ?
  • 36. 36 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
  • 37. 37 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
  • 38. 38 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
  • 39. 39 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;
  • 42. void pointer • Suppose we have to declare integer pointer, character pointer and float pointer, then we need to declare 3 pointer variables. • Instead of declaring different types of pointer variable it is feasible to declare single pointer variable which can act as integer pointer, character pointer. • In C, General Purpose Pointer is called as void Pointer. • It does not have any data type associated with it. • It can store address of any type of variable. • A void pointer is a C convention for a raw address. • The compiler has no idea what type of object a void Pointer really points to? • Declaration of void pointer : void *pointer_name; • Why use void pointer ?  Reusability of pointers 42
  • 43. void pointer • void *ptr; // ptr is declared asVoid pointer char cnum; int inum; float fnum; ptr = &cnum; // ptr has address of character data ptr = &inum; // ptr has address of integer data ptr = &fnum; // ptr has address of float data • We have declared 3 variables of integer, character and float type. • When we assign address of integer to the void pointer, pointer will become Integer Pointer. • When we assign address of Character Data type to void pointer it will become Character Pointer. • Similarly we can assign address of any data type to the void pointer. • It is capable of storing address of any data type 43
  • 44. Pointers and Arrays • int arr[4]; • Name of the array is actually a pointer that points to the first element of the array. • Here, address of first element of an array is &arr[0]. • Also, arr represents the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to arr. • Also, value inside the address &arr[0] and address arr are equal. • Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] is equivalent to *arr. 44
  • 45. Pointers and Arrays • Similarly, • &a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1). • &a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2). • &a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3). • . • &a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i). 45
  • 49. Pointers and Arrays • Program for accessing array elements and printing array elements in reverse using pointer. 49
  • 50. Some programs on arrays using pointer • Program for accessing array elements and print array elements in reverse order using pointer. • Program for sorting n numbers in an array using pointer. • Program for finding reverse of a string using pointer. • Program to manipulate string using pointer (i.e find length, compare, concatenate and copy string) 50
  • 51. Pointers and functions – Call by reference• When, argument is passed using pointer, address of the memory location is passed instead of value. • The address of memory location num1 and num2 are passed to function and the pointers *a and *b accept those values. So, the pointer a and b points to address of num1 and num2 respectively. When, the value of pointer are changed, the value in memory location also changed correspondingly. Hence, change made to *a and *b was reflected in num1 and num2 in main function. 51
  • 52. Function returning a pointer • Till now, we have seen functions that take arguments as pointers and returning only the standard data type. • We can have a function which does not return a value but a pointer to a value. • The syntax is data_type *func_name(arguments); • Here, * before the name of a function means that the functions returns a pointer of the data_type mentioned. So, in the calling function the value must be assigned to a pointer of the appropriate type. 52
  • 53. Function returning a pointer • In this case, must be careful because local variables of function does not live outside the function, hence if you return a pointer connected to a local variable, that pointer will be pointing to nothing when function ends. 53
  • 54. Advantages of Pointers 1. Pointers are more efficient in handling arrays and data tables. 2. Pointers reduce length and complexity of programs. 3. They increase the execution speed and thus reduce the program execution time. 4. Pointers can be used to return multiple values from a function via function arguments. 5. The use of pointer arrays to character strings results in saving of data storage space in memory. 6. Pointer permit references to functions and thereby facilitating passing of functions as arguments to other functions. 7. Pointers allow C to support dynamic memory management. 8. Pointers provide an efficient tool for manipulating dynamic structures such as structures, linked lists, stacks. 54
  • 55. Disadvantages of Pointers 1. Uninitialized pointers might cause segmentation fault. 2. Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak. 3. Pointers are slower than normal variables. 4. If pointers are updated with incorrect values, it might lead to memory corruption 55