Professional Documents
Culture Documents
Objectives
• Appreciate the need and use of pointers.
• Void Pointer
• NULL Pointer
• Double Pointer
• Sizeof operator
• Operations on Pointers
The unary operator & gives the address of an object, so the statement p = &c assigns the
address of c to the variable p, and p is said to ``point to'' c
The & operator only applies to objects in memory: variables and array elements. It cannot be
applied to expressions, constants, or register variables
Note : int k =2;
There are two “values” associated with the object k.One is the value of the integer stored
there (rvalue) and the other “value” of the memory location, i.e. the address of k (lvalue)
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
3
Pointers and Addresses
E.g.
int x = 1, y = 2;
int *ip; /* ip is a pointer to int */
ip = &x; /* ip now points to x */
y = *ip; /* y is now 1 */
*ip = 0; /* x is now 0 */
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
4
Pointers - Example
int main()
{
int x, *p;
• The solution is to use the special type of pointer variable introduced for this purpose.
This is ‘pointer to void’ typically called void pointer.
Address of different types of variables can be assigned in to void pointer. But we can not
dereference a void pointer.
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
7
void Pointer - Example
int main()
{
int num = 10, *iptr;
char ch = 'A', *cptr;
void *vptr; /* void pointer declaration*/
iptr = #
printf("%d\n",*iptr); /* Prints 10 */
cptr = &ch;
printf("%c\n",*cptr); /* Prints A */
printf("%c\n", *((char *) vptr)); /* There is no issue since the void pointer is type casted to correct type*/
printf("%d\n", *((int *) vptr)); /* Undefined value since the vptr is type casted to correct type */
}
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
8
Size of Pointer
Size of different data types can be found out using the “sizeof” operator.
Can we use the same for pointer variable also ? Yes. Look at the following example.
int main()
{
int number = 0;
int *pointer;
number = 10;
pointer = &number;
printf("\ninteger’s size: %d bytes", sizeof(number)); /* Outputs the size of int */
printf("\npointer's size: %d bytes", sizeof(pointer)); /* Outputs the size of pointer */
}
Find the size of char and float pointers as well and check if they are same as that of int pointer.
They are same in fact but actual size of pointer variable depends on the underlying
platform and it can vary from one to other. See, on 32 bit machine it can be 4 bytes but on 64 bit
machine it can be 8 bytes.
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
9
Operations on Pointers
Following are the operations which are possible on pointers
– Referencing (&)
– Dereferencing (*)
– Multiplication of pointers
– Division of pointers
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
10
NULL Pointers
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
12
Double Pointers : Contd
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
13
Double Pointers - Example
int main()
{
int num1 = 11, num2 = 22, num3 = 33;
int *i_ptr1 = &num1; /* i_ptr1 has the address of num1 */
int *i_ptr2 = &num2; /* i_ptr2 has the address of num2 */
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
14
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
Data Table
pptr = &ptr;
*ptr = 3; Name Type Description Value
**pptr = 9;
*pptr = &i;
*ptr = -2;
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
15
Pointers - An Illustration
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
16
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr; Data Table
*ptr = 3; Name Type Description Value
**pptr = 7; i int integer variable 5
ptr = &j; j int integer variable 10
**pptr = 9; ptr int * integer pointer variable
*pptr = &i; pptr int ** integer pointer pointer variable
*ptr = -2;
Double
Indirection
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
17
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 5
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
18
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3;
Data Table
**pptr = 7;
Name Type Description Value
ptr = &j;
i int integer variable 5
**pptr = 9;
j int integer variable 10
*pptr = &i;
ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
19
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
Data Table
pptr = &ptr;
*ptr = 3; Name Type Description Value
*pptr = &i; pptr int ** integer pointer pointer variable address of ptr
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
20
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name Type Description Value
**pptr = 7;
i int integer variable 7
ptr = &j;
j int integer variable 10
**pptr = 9;
ptr int * integer pointer variable address of i
*pptr = &i;
pptr int ** integer pointer pointer variable address of ptr
*ptr = -2;
**pptr int de-reference of de-reference of pptr 7
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
21
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
Data Table
pptr = &ptr;
Name Type Description Value
*ptr = 3;
i int integer variable 7
**pptr = 7;
j int integer variable 10
ptr = &j;
ptr int * integer pointer variable address of j
**pptr = 9;
pptr int ** integer pointer pointer variable address of ptr
*pptr = &i;
*ptr int de-reference of ptr 10
*ptr = -2;
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
22
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
Data Table
pptr = &ptr;
Name Type Description Value
*ptr = 3;
i int integer variable 7
**pptr = 7;
j int integer variable 9
ptr = &j;
ptr int * integer pointer variable address of j
**pptr = 9;
pptr int ** integer pointer pointer variable address of ptr
*pptr = &i;
**pptr int de-reference of de-reference of pptr 9
*ptr = -2;
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
23
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; Data Table
*ptr = 3; Name Type Description Value
**pptr = 7; i int integer variable 7
ptr = &j; j int integer variable 9
**pptr = 9; ptr int * integer pointer variable address of i
*pptr = &i; pptr int ** integer pointer pointer variable address of ptr
*ptr = -2; *pptr int * de-reference of pptr value of ptr
(address of i)
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
24
Pointers - An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; Data Table
*ptr = 3; Name Type Description Value
**pptr = 7; i int integer variable -2
ptr = &j; j int integer variable 9
**pptr = 9; ptr int * integer pointer variable address of i
*pptr = &i; pptr int ** integer pointer pointer variable address of ptr
*ptr = -2; *ptr int de-reference of ptr -2
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
25
Pointers - Assignment
– Declare a void pointer and assign each of the above pointers one
by one to it and print the variable value using void pointer.
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
26
Disclaimer
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
27
Thank you