You are on page 1of 28

Pointers in C

Objectives
• Appreciate the need and use of pointers.

 Use and Appreciate the following :

• Void Pointer

• NULL Pointer

• Double Pointer

• Sizeof operator

• Operations on Pointers

• Call function by Pass by reference


“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
Pointers and Addresses
 A pointer is a variable suitable for keeping memory addresses of other variables, the values
you assign to a pointer are memory addresses of other variables (or other pointers).
 if c is a char and p is a pointer that points to it, we could represent the situation in the
following way:

 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

 The unary operator * is the indirection or dereferencing operator;


when applied to a pointer, it accesses the object the pointer
points to

 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;

p = &x; /* initialise pointer */


*p = 0; /* set x to zero */
printf("x is %d\n", x); /* 0 will be printed here */
printf("*p is %d\n", *p); /* again zero will be printed*/

*p += 1; /* increment what p points to */


printf("x is %d\n", x); /* 1 will be printed here*/

(*p)++; /* increment what p points to */


printf("x is %d\n", x); /* 2 will be printed here*/ }
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
5
What actually a pointer is?

 Pointer is a variable storing an address. In this example “ptr”


is a pointer.
 Pointer is NOT storing the actual value of a variable, but its
address. In this example “ptr” stores the address of variable
int i = 5;
“i”.
int *ptr;
ptr = &i; ptr address of i
printf(“i = %d\n”, i);
printf(“*ptr = %d\n”, *ptr);
printf(“ptr = %p\n”, ptr);
i 5
Output:
(%p is used to print the
address) i = 5 value of ptr =
*ptr = 5 address of i
in memory
ptr = effff5e0
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
6
void Pointers
• A pointer to char is a different type of pointer from a pointer to int and you cannot assign
one to the other, compare them, substitute one for the other as an argument to a function.
There are no implicit conversions from one type of pointer to the another type of pointer.

• It is sometimes necessary to store/copy/move pointers without regard to the type it


references

• So what to do ? If we want to assign addresses of variables of different data types to the


same pointer variable ,is there any solution?

• 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 */

iptr = cptr; /* Incompatible pointer assignment */


printf("%d\n",*iptr); /* Undefined value will be printed */

vptr = cptr; /*There is no issue */


printf("%c\n",*vptr); /* Error: void pointer can not be de-refenced */

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 (*)

– Subtraction of two pointers/ decrementing pointer by some constant

– Incrementing a pointer by a constant

 Following operations are not possible

– Addition of two pointers

– 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

 You may also want a pointer that is guaranteed not to point to


any object—the so-called null pointer. It's common practice in
C to write routines that return pointers. If, for some reason,
they can't return a valid pointer (perhaps in case of an error),
then they will indicate failure by returning a null pointer. An
example could be a table lookup routine, which returns a
pointer to the object searched for if it is in the table, or a null
pointer if it is not.
Declaration :
– int *p;
– P = NULL;
 NULL is a macro defined in the header file stdlib.h
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
11
Double pointers

 Double pointers are pointers to other pointers .


The declaration of a pointer-to-pointer looks like
int **ipp;
where the two asterisks indicate that two levels of indirection is involved.
 For eg.
int i = 5, j = 6,k = 7;
int *ip1 = &i, *ip2 = &j;
int **ipp = &ip1;
 So ipp points to ip1 which points to i. *ipp is ip1, and **ipp is i,
ie 5.

“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
12
Double Pointers : Contd

 If we say *ipp = ip2; we've changed the pointer pointed to


by ipp (that is, ip1) to contain a copy of ip2, so that it (ip1)
now points at j:


 If we say *ipp = &k; we've changed the pointer pointed to by


ipp (that is, ip1 again) to point to k:

“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 */

int **p_ptr = &i_ptr1; /* p_ptr has the addess of i_ptr1 */

printf("%d\n", **p_ptr); /* Prints the value 11 */

*p_ptr = i_ptr2; /* Now i_ptr1 and i_ptr2 both points to num2 */


printf("%d\n", **p_ptr); /* Prints the value 22 */
printf("%d\n", *i_ptr1); /* Prints the value 22 */
printf("%d\n", *i_ptr2); /* Prints the value 22 */

*p_ptr = &num3; /*Now i_ptr1 has the address of num3 */


printf("%d\n", **p_ptr); /* Prints the value 33 */
printf("%d\n", *i_ptr1); /* Prints the value 33 */
printf("%d\n", *i_ptr2); /* Prints the value 22 */

**p_ptr = 44; /* Value of num3 changed to 44 */


printf("%d\n", num3);

*i_ptr1 = 55; /* Value of num3 changed to 55 */


printf("%d\n", num3);
}

“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 = 7; i int integer variable 5

ptr = &j; j int integer variable 10

**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 = 7; i int integer variable 3

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; *ptr int de-reference of ptr 3

“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

 Write a program which accepts character, integer and float


values.

– Declare pointers to each of the above variables

– Display the size of variables and pointer on a 32-bit machine and


64-bit machine.

– 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

 “Aricent makes no representations or warranties with respect to


contents of these slides and the same are being provided “as
is”.  The content/materials in the slides are of a general nature
and are not intended to address the specific circumstances of
any particular individual or an institution; any materials not
specifically acknowledged is purely unintentional”

“The contents here are for Aricent internal training purposes only and do not carry any commercial value”
27
Thank you

You might also like