Dereferencing Pointers
This note is about the Dereferencing Pointers used in C programming language. It explains the pointers to pointers, pointers, and arrays, passing arrays to the function. It also explains the Arrays of pointers and Pointers and Strings too.
Summary
This note is about the Dereferencing Pointers used in C programming language. It explains the pointers to pointers, pointers, and arrays, passing arrays to the function. It also explains the Arrays of pointers and Pointers and Strings too.
Things to Remember
- Dereferencing is an operation performed to access and manipulate data obtained in the memory location pointed by a pointer.
- The programmer should note that a pointer defined as int cannot hold the data type defined as float or char.
- 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.
- With the help of pointer, we can access the data at the address.
- Pointers to pointers offer flexibility in handling arrays, passing the pointer variable to functions, etc.
- 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.
MCQs
No MCQs found.
Subjective Questions
No subjective questions found.
Videos
No videos found.

Dereferencing Pointers
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.
- 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
- p = p – b
- p = (int*) (p-q);
- p = (int*) (p-q)-a;
- p = p+a
Invalid Pointer Arithmetic
- int a, b, *p, *q; /*Illegal use of pointers */
- p = - q; /*Illegal use of pointers */
- p = p - q; /*Illegal use of pointers conversion*/
- 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:
- Array elements are always stored in contiguous memory locations irrespective of the size of the array.
- The size of the data type which the pointer variable refers to is dependent on the data type pointed by the pointer.
- 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.
- Array elements are always stored in contiguous memory locations irrespective of their sizes.
- The size of the data type which the pointer variable refers to is dependent on the data type pointed to by the pointer.
- 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
Lesson
Programming in C
Subject
Computer Science
Grade
Grade 12
Recent Notes
No recent notes.
Related Notes
No related notes.