CPointer New

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 57

Pointers in C

1
Features of Pointers
 Pointers provide direct access to memory
 Reduces the storage space and complexity of the
program.
 Reduces the execution time of the program.
 Provides an alternative way to access array
elements.
 Pointers help us to build complex data structures
like linked lists, queues, stacks, tree, graph etc.

2
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 = %d\n”, *ptr); /* refer to referee of ptr */

3
What actually ptr is?

 ptr is a variable storing an address


 ptr is NOT storing the actual value of i
ptr address of i
int i = 5;
int *ptr;
ptr = &i; i 5
printf(“i = %d\n”, i); Output:
printf(“*ptr = %d\n”, *ptr); i = 5 value of ptr =
printf(“ptr = %p\n”, ptr); *ptr = 5 address of i
in memory
ptr = effff5e0

4
Twin Operators

 &: Address-of operator


 Get the address of an entity
 e.g. ptr = &j;

 *: De-reference operator
 Refer to the content of the referee
 e.g. *ptr = 99;

5
Example: Pass by Reference
 Modify behaviour in argument passing
void f(int i) void f(int *ptr)
{ {
i = 5; *ptr = 5;
} }
void main() void main()
{ {
int i = 3; int i = 3;
f(i); f(&i);
} i = 3? } i = 5?

6
An Illustration
int i = 5, j = 10;
int *ptr;
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 = -2;

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

8
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; 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;
pptr int ** integer pointer pointer variable

Double
9
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
10
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) 11
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 3
**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
*ptr int de-reference of ptr 3
12
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**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 de-reference of 7
pptr 13
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 10
14
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of 9
pptr 15
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*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) 16
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable -2
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr -2
17
C pointer variable memory
required
 Pointer Variable Stores the address of the variable.
 Variable may be integer, character, float but
the address of the variable is always integer so
Pointer requires 2 bytes of memory in Turbo C
Compiler.
 If we run same program in any other IDE then the
output may differ.
 This requirement is different for different Compilers.

18
NULL Pointers

 It is always a good practice to assign a NULL


value to a pointer variable in case you do not
have an exact address to be assigned.
 This is done at the time of variable declaration.
 A pointer that is assigned NULL is called
a null pointer.
 The NULL pointer is a constant with a value of
zero defined in several standard libraries.

19
NULL Pointers

#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
Output: The Value of ptr is: 0

20
Pointer Arithmetic

 We can perform arithmetic operations on


pointer variable just as you can a numeric value.

 The arithmetic operations on pointer variable


effects the memory address pointed by pointer.

21
Valid Pointer Arithmetic
Operations
 Adding a number to pointer.
 Subtracting a number form a pointer.
 Incrementing a pointer.
 Decrementing a pointer.
 Subtracting two pointers.
 Comparison on two pointers.

22
Invalid Pointer Arithmetic
Operations
 Addition of two pointers.
 Division of two pointers.
 Multiplication of two pointers.

23
Incrementing a Pointer
 Let ptr be an integer pointer which points to the
memory location 5000 and size of an integer variable
is 32-bit(4 bytes). Now, when we increment pointer
ptr
ptr++; or ptr = ptr+1

 it will point to memory location 5004 because it will


jump to the next integer location which is 4 bytes
next to the current location.

ptr++ is equivalent to ptr+(sizeof(pointer data type))


24
Decrementing a Pointer

 Let ptr be an integer pointer which points to the memory


location 5000 and size of an integer variable is 32-bit(4
bytes). Now, when we increment pointer ptr
ptr--; or ptr = ptr-1

 it will point to memory location 4996 because it will jump


to the next integer location which is 4 bytes next to the
current location.

ptr-- is equivalent to ptr-(sizeof(pointer data type))


25
Adding Numbers to Pointers

 Adding a number N to a pointer leads the pointer to a


new location after skipping N times size of data type.

ptr + N = ptr + (N * sizeof(pointer data type))

For example, Let ptr be a 4-byte integer pointer, initially


pointing to location 5000. Then ptr + 5 = 5000 + 4*5
= 5020. Pointer ptr will now point at memory address
5020.

26
Subtracting Numbers from
Pointers
 Subtracting a number N to a pointer leads the pointer
to a new location before N times size of data type
from current location.

ptr - N = ptr - (N * sizeof(pointer data type))

For example, Let ptr be a 6-byte double pointer, initially


pointing to location 5000. Then ptr - 5 = 5000 - 6*5 =
4970. Pointer ptr will now point at memory address
4970.
27
Subtracting Pointers
 The difference between two pointer returns indicates
“How apart the two Pointers are. It gives the total
number of elements between two pointers.

 For example, Let size of integer is 4 bytes. If an


integer pointer 'ptr1' points at memory location
10000 and integer pointer 'ptr' points at memory
location 10008, the result of ptr2 - ptr1 is 2.

28
Subtracting Pointers

int main()
{
int *ptr, *ptr1;
ptr=2000;
ptr1=2020;
int x=ptr1-ptr;
printf("%d",x);
return 0;
} 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
int int_var = 10, *int_ptr;
char char_var = 'A', *char_ptr;
float float_val = 4.65, *float_ptr;
/* Initialize pointers */
int_ptr = &int_var;
char_ptr = &char_var; EXAMPLE
float_ptr = &float_val;
printf("Address of int_var = %u\n", int_ptr);
printf("Address of char_var = %u\n", char_ptr);
printf("Address of float_var = %u\n\n", float_ptr);
/* Incrementing pointers */
int_ptr++;
char_ptr++;
float_ptr++;
printf("After increment address in int_ptr = %u\n", int_ptr);
printf("After increment address in char_ptr = %u\n", char_ptr);
printf("After increment address in float_ptr = %u\n\n", float_ptr);
/* Adding 2 to pointers */
int_ptr = int_ptr + 2;
char_ptr = char_ptr + 2;
float_ptr = float_ptr + 2;
printf("After addition address in int_ptr = %u\n", int_ptr);
printf("After addition address in char_ptr = %u\n", char_ptr);
printf("After addition address in float_ptr = %u\n\n", float_ptr); 31
Example Output

32
Comparison between two
Pointers
 Pointer comparison is Valid only if the two
pointers are Pointing to same array.
 All Relational Operators can be used for comparing
pointers of same type.
 All Equality and Inequality Operators can be
used with all Pointer types.
 Pointers cannot be Divided or Multiplied.

33
Example

int main()
{
int *ptr1,*ptr2;
ptr1 = 1000;
ptr2 = 2000;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}
34
Example
int main()
{
int i, j;
int *ptr1,*ptr2;
ptr1 = &i;
ptr2 = &j;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}
35
Example
int main()
{
int a[3] = {1, 2, 3};
int *ptr1,*ptr2;
ptr1 = &a[1];
ptr2 = &a[2];
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}
36
Example
int main()
{
int a[3] = {1, 2, 3};
int *ptr1,*ptr2;
ptr1 = &a[1];
ptr2 = &a[0];
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}
37
Example

int main()
{
int *ptr1,*ptr2;
ptr1 = 1000;
ptr2 = ptr1/2;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
return(0);
}
38
Comparison Operators

> Greater Than


< Less Than
>= Greater Than And Equal To
<= Less Than And Equal To
== Equals
!= Not Equal

39
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 and float pointer.

40
Void 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 ?

41
Example Void Pointer

42
Example Void Pointer

43
Example Void Pointer

44
Example Void Pointer

#include<stdio.h>
int main()
Compiler Error: 'void*' is not a
{ pointer-to-object type
int a = 10;
void *ptr = &a;
printf("%d", *ptr);
return 0;
}
45
Example Void Pointer

46
(++*ptr) vs (*++ptr) pointer

int main()
{
int n = 20 , *ptr ;
ptr = &n;
printf("%d",*++ptr);
}

47
(++*ptr) vs (*++ptr) pointer

int main()
{
int n = 20 , *ptr ;
ptr = &n;
printf("%d",++*ptr);
}

48
(++*ptr) vs (*++ptr) pointer

 ‘++’ and ‘*’ both have Equal Precedence.

 Associativity is from Right to Left ( Expression


evaluated from R->L).

 Both are Unary (Operates on single operand ).

 So ‘++’ is Performed First then ‘*’ in (*++ptr)


but ‘ * ’ is performed First then ‘++’ in (++*ptr).
49
Pointer to array in C

 Array and Pointer are backbones of the C


Programming language.

 Pointer and array , both are closely related.

 We know that array name without subscript


points to first element in an array.

50
Pointer to array in C

Example : One dimensional array


int a[10];
Here a , a[0] both are identical

Example : Two dimensional Array


int a[10][10];
Here a , a[0][0] both are identical
51
Pointer to array in C

Let
x is an array ,
i is subscript variable
x[i] = *(x+i)
= *(i+x)
= i[x]

52
Pointer to array in C

 For first element of array i = 0


x[0] = * ( x + 0)
= *x
 Thus *(x+i) will gives us value of
ith element.
 2-D Array can be accessed –
x [ i ][ j ] = *(x[i] + j)
= *( * ( x + i ) + j )

53
C pointer expression for a[i]

 Consider we are having the array – say arr[6]. We


know that we can access the first element of an
array using the a[0].

int arr[] = {11,22,33,44,55,66}

54
C pointer expression for a[i]

Array Element Element Accessed Element

arr[0] Accessing first element of an array 11

arr[1] Accessing second element of an array 22

arr[2] Accessing third element of an array 33

arr[3] Accessing fourth element of an array 44

arr[4] Accessing fifth element of an array 55

arr[5] Accessing sixth element of an array 66

55
C pointer expression for a[i]

Equivalent pointer
Array Element
expression
arr[0] *(arr+0)
arr[1] *(arr+1)
arr[2] *(arr+2)
arr[3] *(arr+3)
arr[4] *(arr+4)
arr[5] *(arr+5)
56
int main()
{
int x[] = {1,2,3,4,5};
int *ptr, i ;
ptr = x; //copying the address of the array to ptr variable
for(i=0;i<5;i++) {
printf("nAddress : %u\n", &x[i]);
printf("nElement : %d\n", x[i]);
printf("nElement : %u\n", *(x+i));
printf("nElement : %d\n", i[x]);
printf("nElement : %d\n", *(ptr+i));
}
} 57

You might also like