ACP 1 2 Pointers-V1

28
Pointers in C 

Transcript of ACP 1 2 Pointers-V1

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 1/28

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 2/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 

Obj ectives

� Appreciate the need and use of pointers.

U se and Appreciate the following :

� Void Pointer 

� N ULL Pointer 

� Dou  ble Pointer 

� Sizeof operator 

� O perations on Pointers

� Call function by Pass by reference

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 3/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 3

Pointers and Addresses

 A pointer is a variable suitable for keeping memor y addresses of other variables, the values

y ou assign to a pointer are memor y 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 unar y operator & gives the address of an obj ect, so the statement p = &c assigns the

address of c to the variable p, and p is said to ``point to'' c 

The & operator onl y applies to obj ects in memor y : 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 obj ect k.One is the value of the integer stored 

there (rvalue) and the other ³value´ of the memor y location, i.e. the address of k (lvalue)

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 4/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 4

Pointers and Addresses

The unar y  operator * is the indirection or dereferencingoperator; when applied to a pointer, it accesses the obj ect 

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

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 5/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 5

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

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 6/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 6

W hat actuall y a pointer is?

Pointer is a variable storing an address. In this example ³ptr´ is a pointer.

Pointer is N OT storing the actual value of a variable, but its

address. In this example ³ptr´ stores the address of variable

³i´.int i = 5;

int * ptr;

 ptr = &i;

 printf(³i = %d\n´, i);

 printf(³*ptr = %d\n´, *ptr);

 printf(³ptr = %p\n´, ptr);

(%p is used to print the

address)

5i

address of i ptr 

Output:

i = 5

*ptr = 5

ptr = effff5e0

value of ptr =

address of i

in memory

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 7/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 7

void Pointers

� A pointer to char is a different t y  pe of pointer from a pointer to

int and y ou cannot assign one to the other, compare them,

su bstitute one for the other as an argument to a function.

There are no implicit conversions from one t y  pe of pointer to

the another t y  pe of pointer.

� It is sometimes necessar y to store/copy  /move pointers

without regard to the t y  pe it references

� So what to do ? If we want to assign addresses of variables of 

different data t y  pes to the same pointer variable ,is there any  

solution?

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 8/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 8

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; /* Incompati ble pointer assignment */ 

 printf("%d\n",*iptr); /* U ndefined 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 t y  pe casted to correct t y  pe*/ 

 printf("%d\n", *((int *) vptr)); /* U ndefined value since the vptr is t y  pe casted to correct t y  pe */ 

 }

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 9/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 9

Size of Pointer 

Size of different data t y  pes 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 by tes", sizeof(number)); /* Outputs the size of int */  printf("\npointer's size: %d by tes", 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 underl y ing

 platform and it can var y from one to other. See, on 32 bit machine it can be 4 by tes but on 64bit machine it can be 8 by tes.

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 10/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 10

O perations on Pointers

Following are the operations which are possi ble on pointers

 ± Referencing (&)

 ± Dereferencing (*)

 ± Su  btraction of two pointers/ decrementing pointer by some constant 

 ± Incrementing a pointer  by a constant 

Following operations are not possi ble

 ± Addition of two pointers

 ± Multiplication of pointers

 ± Division of pointers

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 11/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 11

N ULL Pointers

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

any obj ect²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 obj ect searched for if it is in the table, or a null  

 pointer if it is not.

Declaration : ± int *p;

 ± P = N  ULL;

N ULL is a macro defined in the header file stdli b.h

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 12/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 12

Dou ble pointers

Dou ble 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.

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 13/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 13

Dou ble Pointers : Contd 

If we say *ipp = ip2; we've changed the pointer pointed toby 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:

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 14/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 14

Dou ble 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);

 }

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 15/28

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

int **pptr;

 ptr = &i;

 pptr = &ptr;

*ptr = 3;

**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 5 

 j  int integer variable 10  

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 16/28

³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; /* 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 T  y  pe Description Value

i int integer variable 5 

 j  int integer variable 10  

 ptr int * integer pointer varia ble

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 17/28

³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; /* 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 T  y  pe Description Value

i int integer variable 5 

 j  int integer variable 10  

 ptr int * integer pointer varia ble

 pptr int ** integer pointer pointer varia ble

Double

Indirection

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 18/28

³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; /* store address-of i to ptr */

 pptr = &ptr;

*ptr = 3;

**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 5 

 j  int integer variable 10  

 ptr int * integer pointer varia ble address of i  pptr int ** integer pointer pointer varia ble

*ptr int de-reference of ptr 5  

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 19/28

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

 pptr = &ptr; /* store address-of ptr to pptr */

*ptr = 3;**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 5 

 j  int integer variable 10  

 ptr int * integer pointer varia ble address of i   pptr int ** integer pointer pointer varia ble address of ptr 

*pptr int * de-reference of pptr value of ptr  

(address of i)

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 20/28

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

*ptr = 3;

**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 3

 j  int integer variable 10  

 ptr int * integer pointer varia ble address of i 

 pptr int ** integer pointer pointer varia ble address of ptr  

*ptr int de-reference of ptr 3

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 21/28

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

 pptr = &ptr;

*ptr = 3;**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 7 

 j  int integer variable 10  

 ptr int * integer pointer varia ble address of i 

 pptr int ** integer pointer pointer varia ble address of ptr **pptr int de-reference of de-reference of pptr 7  

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 22/28

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

 pptr = &ptr;

*ptr = 3;**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 7 

 j  int integer variable 10 

 ptr int * integer pointer varia ble address of  j 

 pptr int ** integer pointer pointer varia ble address of ptr  

*ptr int de-reference of ptr 10  

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 23/28

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

*ptr = 3;

**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 7 

 j  int integer variable 9

 ptr int * integer pointer varia ble address of  j 

 pptr int ** integer pointer pointer varia ble address of ptr 

**pptr int de-reference of de-reference of pptr 9

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 24/28

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

*ptr = 3;

**pptr = 7;

 ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name T  y  pe Description Value

i int integer variable 7 

 j  int integer variable 9

 ptr int * integer pointer varia ble address of i 

 pptr int ** integer pointer pointer varia ble address of ptr 

*pptr int * de-reference of pptr value of ptr  

(address of i)

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 25/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 25

Pointers - 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 T  y  pe Description Value

i int integer variable -2 

 j  int integer variable 9

 ptr int * integer pointer varia ble address of i 

 pptr int ** integer pointer pointer varia ble address of ptr  

*ptr int de-reference of ptr -2  

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 26/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 26

W rite 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 oneby one to it and print the variable value using void pointer.

Pointers - Assignment 

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 27/28

³The contents here are for Aricent internal training purposes only and do not carry any commercial value´ 27

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 specificall y acknowledged is purel y unintentional´ 

8/6/2019 ACP 1 2 Pointers-V1

http://slidepdf.com/reader/full/acp-1-2-pointers-v1 28/28

Thank y ou