1 of 22

POINTERS

1

26/12/22

2 of 22

Declaration – a point about pointers

2

26/12/22

  • int j, k, l;
  • j, k, l are integer type.
  • we can say that int type declaration distributes over the list of names j, k, l
  • int *p, q, r;
  • Here, only p is int pointer
  • q and r are ordinary int variables
  • The indirection operator (*) does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name.

3 of 22

Pointing to nothing !

3

26/12/22

  • NULL is a symbolic constant defined in

<stdio.h> with 0 (zero)

  • A pointer with the value NULL points to nothing.
  • The value 0 (NULL) is the only integer value that can be assigned directly to a pointer variable.
  • int *p = NULL;

*p = 5; /* this results in run-time error */

4 of 22

Functions

4

26/12/22

  • A function can return only one value back to the calling function.

  • If you want more values to be communicated back to the calling function, then you have to use addresses and pointers.

5 of 22

Functions – an example

5

26/12/22

void find_min_max_marks(float *, float *, float *, int); main( )

{

float min, max, marks[40];

… …;

find_min_max_marks(marks, &min, &max, 40);

}

void find_min_max_marks(float *m, float *a, float *z, int size)

{

int j;

*z = m[0]; *a = m[0]; for (j=1; j<size; j++) {

if(m[j] < *a) *a = m[j];

if(m[j] > *z) *z = m[j];

}

}

6 of 22

Comparing two strings

6

26/12/22

  • strcmp(s, t) is a function available in the standard library (<string.h>) that compares the character strings s and t.
  • It returns an integer which is
    • 0 (zero) 🡺 s and t are same
    • Negative 🡺 s is lexicographically less than t
    • Positive 🡺 s is lexicographically larger than t
  • The return value is obtained by subtracting the characters at the first position where s and t disagree.

7 of 22

strcmp – array version

7

26/12/22

int strcmp(char *s, char *t)

{

int j;

for(j=0; s[j] == t[j]; j++)

if( s[j] == ‘\0’) return 0;

return s[j] – t[j] ;

}

8 of 22

strcmp – pointer version

8

26/12/22

int strcmp( char *s, char *t)

{

for ( ; *s == *t; s++, t++) if(*s == ‘\0’) return 0;

return *s - *t;

}

9 of 22

const with pointers

9

26/12/22

  • Passing addresses to functions could be dangerous. By mistake that function can modify the original variables value.
  • To overcome this kind of problems, one can declare that the contents pointed by the pointer are constant.
  • One can also force to say that a pointer has to point to the same location (whatever be contents of the location).

10 of 22

const with pointers

10

26/12/22

  • const can be used in 4 ways with pointers
    • a non-constant pointer to non-constant data
    • a constant pointer to non-constant data
    • a non-constant pointer to constant data
    • a constant pointer to constant data

11 of 22

A non-constant pointer to non- constant data

11

26/12/22

  • int *p, j = 10, k = 20; p = &j;

*p = 15; /* contents pointed by p is modified */ p = &k; /* p is modified */

12 of 22

A constant pointer to non-constant data

12

  • Pointer always points to the same memory location. But contents at that memory can change.
    • int x, y;

int * const ptr = &x;

*ptr = 7; /* OK same as x = 7*/ ptr = &y; /* error */

13 of 22

A constant pointer to non-constant data

13

26/12

  • An array name is a constant pointer to the beginning of the array.
  • All the data in the array can be accessed and changed by using the array name and array subscripting.
  • But array name cannot point to some other location.

14 of 22

A non-constant pointer to constant data

14

  • pointer can be modified to point to some other location.
  • But the contents using the pointer (with indirection operator *) can not be modified.
  • This is especially useful with functions.

15 of 22

A non-constant pointer to constant data

15

  • const int * ptr; int j = 10, k;

ptr = &j; /* OK */

*ptr = 25; /* error */ j = 25; /* OK */

ptr = &k; /* OK */

*ptr = 100; /* error */ k = 100; /* OK */

j = *ptr; /* OK */

16 of 22

A non-constant pointer to constant data

  • void f( const int *); int main( )

{

int y = 111; f(&y);

}

void f( const int * ptr)

{

*ptr = 100; /* an error occurs */

}

16

17 of 22

A constant pointer to constant data

17

  • int x = 5, y;

const int *const ptr = &x;

y = *ptr; ptr = &y;

/* OK */

/* error */

*ptr = 20; /* error */

  • If you want to pass an array whose contents should not be modified, then use this kind of declarations.

18 of 22

Pointer Expressions and Pointer Arithmetic

18

  • A limited set of arithmetic operations may be performed with pointers
    • A pointer may be incremented (++) or decremented ( - - )
    • An integer may be added to a pointer (+ or += )
    • An integer may be subtracted from a pointer ( – or – = )
    • One pointer may be subtracted from another.
  • Pointer arithmetic is machine dependant.

19 of 22

Increment ++ and decrement --

19

  • int *ptr, j = 5; int a[20];

ptr = a;

ptr ++; /* ptr now points to a[1] */

*ptr = 10; /* same as a[1] = 10 */ ptr = &j;

ptr ++;

/* now ptr points to next integer after j */

  • Pointer arithmetic is meaningful only with arrays

20 of 22

Subtracting a pointer from other

20

  • double d[20]; double *a, *b; int j;

a = &d[4];

b = &d[10];

j = b – a; /* j gets value 6 */

  • Again, it would be meaningless if a and b are pointing to non-array elements.

21 of 22

Assigning a pointer to another – Void pointer

21

  • A pointer can be assigned to another pointer if both pointers are of the same type.
  • Otherwise, a type cast operator must be used.
  • int *p; char *c;

p = (int *) c;

  • The exception to this rule is for pointer whose type is void *

22 of 22

void pointer

22

  • void *ptr; int i; ptr = &i;
  • Here ptr can contain an address, but the type of the value stored at that address is unknown and hence, dereferencing using ptr is disallowed.
  • *ptr = 5; /* error */
  • *((int *)ptr) = 5; /* Ok, type cast and then

assign*/