Note on Dereferencing Pointers

  • Note
  • Things to remember

INTRODUCTION

A pointer variable points to the address in the memory. A pointer can be used to access the data from that memory where the pointer points. Dereferencing is an operation performed to access and manipulate data obtained in the memory location pointed by a pointer.

  1. int a, b, *p, *q; /*Illegal use of pointers */

A pointer variable is dereferenced when the unary operator * is used as a prefix to the pointer variable. Any operation performed to the dereference pointer directly affects the value of the variable it points to.

Void Pointers

The programmer should note that a pointer defined as int cannot hold the data type defined as float or char. Hence, pointers defined to be specific data type cannot hold the address of any other type of variable.

Pointer Arithmetic

It is true that the size of the data type which the pointer variable points to is the number of bytes accessed in memory when the pointer variable is dereference using the indirection operator (*). The size of the pointer variable is dependent on the type of variable pointed by the pointer.

The C language allows arithmetic operations to be performed on pointer variables. Some valid pointer arithmetic and invalid pointer arithmetic are listed below.

Valid Pointer Arithmetic

  1. p = p – b
  2. p = (int*) (p-q);
  3. p = (int*) (p-q)-a;
  4. p = p+a

Invalid Pointer Arithmetic

  1. int a, b, *p, *q; /*Illegal use of pointers */
  2. p = - q; /*Illegal use of pointers */
  3. p = p - q; /*Illegal use of pointers conversion*/
  4. p = p/q /* Illegal use of pointer */

Pointers to Pointers

A pointer is a variable. It is a data. It can hold the address of a variable. With the help of pointer, we can access the data at the address. Similarly, we can also have pointers to pointers just as we have pointers to integers. Pointers to pointers offer flexibility in handling arrays, passing the pointer variable to functions, etc.

The general format of pointing to pointer could be:

<data_type> ** <ptr_to_ptr>;

Here,

data_type is the type of pointer data

**indicates pointer to pointer

Ptr_to_ptr is the variable name of the pointer to pointer

Pointers and Arrays

When we declare an array, the array elements are stored in continuous locations. So, when the pointer is increased, it always points to next location of similar data type.

# Write a program to find the address of given value in the program.

#include <stdio.h>

#include <conio.h>

void main ()

{

clrscr ();

int a[] = {2, 5, 34, 3, 11};

int *p;

p=&a[0]

for(int i=0; i<5; i++)

{

printf(“\n Address value of %d Address = %u”, *p, &p);

*p++;

}

}

Above program holds a set of array integers. They are placed at different locations in the memory. The pointer *p is used to point the location and %u (unsigned format specifier) is used to access the pointer location. Note that ampersand sign & is used before the pointer variable to find the address.

Facts regarding the relation between pointers and arrays are:

  1. Array elements are always stored in contiguous memory locations irrespective of the size of the array.
  2. The size of the data type which the pointer variable refers to is dependent on the data type pointed by the pointer.
  3. A pointer when incremented, always points to a location after skipping the number of bytes required for the data type pointed to by it.

Passing Arrays to Functions

At the time of manipulating arrays with the help of a function, entire array elements are necessarily passed in the function instead of passing one element at a time. This can be achieved by pointing the base address of the array with the array name.

# Write a program to demonstrate that the arrays are passed to the functions.

#include <stdio.h>

#include <conio.h>

void sortit (int *, int);

void main ()

{

int a[20], I, n;

printf(“\n Enter the number of elements less than 21:”);

scanf(“%i”, &n);

printf(“\n Enter the elements:”);

for (i=0; i<n; i++)

scanf(“%i”, a+1);

sortit(a,n);

printf(“\n The sorted array :\n”);

for(i=0; i<n; i++)

printf(“%i”, *(a+1));

}

void sortit(int *b, int n)

{

int i=0, j, f=0, temp;

while (i++<n && !f)

{

f=1;

for(j=0; j<n-1; j++)

if (b[j] >b[j+1])

{

temp = b[j];

b[j] =b[j+1];

b[j+1] =temp;

f=0;

}

}

}

The function sortit actually sorts the original array itself directly. Since the address of the array is passed as an argument to it. No temporary array is created in this case. This type of function is called cal by reference.

Write a program to find the smallest number explicitly defined in the array.

#include <stdio.h>

#include <conio.h>

void display (int*, int);

void main ()

{

int num[] ={ 4, 1, 99, 67, 45, 96, 3, 8, 54, 86};

clrscr ();

printf(“\n Example of passing array to function:”);

display (num, 10);

}

void display (int *n, int j)

{

int i;

int g=*n;

for (i=0; i<=j; i++)

{

if(*n<g)

g=*n;

n++;

}

printf(“\n Smallest number is =%d”, g);

}

Pointer and Array

The pointers can be used to locate the elements in an array. While accessing the elements of an array, we consider the following points.

  1. Array elements are always stored in contiguous memory locations irrespective of their sizes.
  2. The size of the data type which the pointer variable refers to is dependent on the data type pointed to by the pointer.
  3. A pointer which is incremented always points to a location after skipping the number of bytes required for the data type pointed to by it.

Example:

#include <stdio.h>

void main ()

{

int array[5] ={1, 2, 3, 4, 5};

int i, *ptr;

ptr = &array[0];

for (i=0; i<5; i++)

{

printf(“\n %d %d - %d\n”, array[i], ptr, *ptr);

ptr++;

/* increment the pointer */

}

}

Array of Pointers

An array of pointers is similar to an array of any predefined data type. Since pointer variable always contains an address; an array of pointers is a collection of addresses. These can be addresses of ordinary isolated variables or array elements. The elements of an array of pointers are stored in the memory just as the elements of any other kind of array.

Example:

#include <stdio.h>

#include <stdio.h>

main ()

{

int array[3] ={1, 2, 3};

int i, *ptr[3];

for (i=0; i<3; i++)

ptr[i] =array+I;

for(i=0; i<3; i++)

printf(“%p, %d\n”, ptr[i], *ptr[i]);

}

Pointers and Strings

As in integer array, in character array also we get the base address of a string by mentioning character variable to pointer variable. To get other elements of an array, we can increase the value of a pointer. When a pointer is increased, it points to the immediate location of its type, till null character (‘\0’) is not reached.

# Write a program to find the greatest number in an array using pointer variable with a function.

#include <stdio.h>

#include <conio.h>

#include <math.h>

void greatest (int *n, int);

void main ()

{

int num[]= {4, 2, 5, 99, 45, 66, 89, 65, 34};

clrscr ();

greates (&num[0], 9);

}

void greatest (int *n, int j)

{

int m=0;

int I;

for (i=0; i<j; i++)

{

if (*n >m)

{

m= *n;

}

*n++;

}

printf(“\n The greatest number is %d”, m);

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 255-263.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

  1. Dereferencing is an operation performed to access and manipulate data obtained in the memory location pointed by a pointer.
  2. The programmer should note that a pointer defined as int cannot hold the data type defined as float or char. 
  3. The size of the pointer variable is dependent on the type of variable pointed by the pointer.
  4. The C language allows arithmetic operations to be performed on pointer variables.
  5. With the help of pointer, we can access the data at the address.
  6. Pointers to pointers offer flexibility in handling arrays, passing the pointer variable to functions, etc.
  7. The function sort actually sorts the original array itself directly since the address of the array is passed as an argument to it. No temporary array is created in this case. This type of function called is called cal by reference. 
.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

No discussion on this note yet. Be first to comment on this note