The memory of our computer can be considered as succession of memory cells and each one of them has a unique address so that it can be easily located in memory. Pointers in C are basically used to access the value of a variable residing on a particular memory cell. Pointers, or simply a pointer variable, can be defined as a variable which contains or stores the address of another variable in memory.
A pointer is considered to be a very powerful feature of C programming language which has use in advancing programming as C pointers can be used to any variable type of C. In this post, I’ll give a brief description of C pointers, and take you through initialization and declaration, use of pointers in conjunction with functions and arrays, types of C pointers, and overall how to implement pointers in C programming language.
1. Introduction:
Basically there are two ways to access the value of a variable – either by using by name of the variable or by using the address of the variable. Since accessing a value of variable by its name is easier to understand, most of the beginners use the name to access the value of variable. But this method to access the value of variable has a number of demerits such as:
- It creates problems in handling data table.
- A variable outside the function is inaccessible.
- Dynamic Memory Allocation is not possible.
- More than value can’t be returned from a function, etc.
So, in order to overcome these defects while programming, pointers in C were developed as a new parameter. Now, to have a clear concept of value and address of a variable in C programming, consider the following memory map:
In this memory map, the value of variable i is 3 which has been residing at memory cell address of 65524.
The figure shows the concept of C pointers where i_pointer is a pointer variable which stores the address of i, i.e., it is pointing to the variable i.
2. Declaration and Initialization of Pointers in C:
Declaration of C pointers is similar to the declaration of other variable, but the only difference in declaration is the use of dereference operator (*). For example, if you are going to declare a integer variable marks as pointer variable, it is done as:
int*marks;
Again, for the demonstration of initialization pointers in C, consider the following segment of source code:
int len;
int *len_ptr;
len_ptr=&len;
Here, len_ptr is a pointer variable which points to an ordinary variable len. So, whenever a pointer is to be initialized, it should be assigned with the reference or the address of the variable, not with the value of the variable.
3. Pointers Arithmetic in C:
Like other ordinary variables, pointer variables can also be expressed in terms of expression. The pointer expression is similar to ordinary C expressions to some extent, but in the expression of C pointers, there should be proper use of reference and dereference operators. Some rules regarding arithmetic for pointers in C are given below:
- A pointer variable can be assigned to the address of an ordinary variable.
- Pointers can be initialized or assigned as NULL; NULL is the symbolic representation of zero.
- An integer type of variable can be added or subtracted from a pointer variable.
- Pointers in C can’t be divided or subtracted by a constant.
- Two pointer variables can’t be added.
- C pointers cannot be assigned to an ordinary variable.
4. Functions and Pointers in C:
A function can be called either by passing the value of argument or by passing the address of the arguments. If a function is called by passing the address of the argument, it is known as calling a function by reference. While calling a function by reference, C pointers are used, and for this the following points should be kept in mind:
- The parameter of a function must be declared as a pointer variable.
- In the function body, dereference operators should be used to withdraw the values from the memory location or the address.
- While calling a function, argument of the function should be passed as actual arguments.
The example below gives you a clear concept of pointer arithmetic in C as well as the co-ordination of functions and pointers in C:
#include<stdio.h>
void lar(int*, int* int*);
void main()
{
int x,y lar;
printf(“Enter the value of x and y:”);
scanf(“%d%d”, &x,&y);
printf(“&x=%u\n&y=%u\n and large=%u”, &x,&y,&lar);
lar(&x, &y,&lar);
printf(“Larger number is %d”, lar);
}
void lar(int *a, int *b ,int*c)
{
printf(“a=%u\nb=%u\nc=%u\n”,a,b,c);
if(*a>*b)
*c=*a;
else
*c=*b;
}
//OUTPUT:
Enter the value of x and y: 10, 50
&x = 65524
&y=65522
&lar=65520
a=65524
b=65522
c=65520
Larger number is 50//
In the example aforementioned, there is no return statement but the output of the program is as expected. This is due to pointer variables. So, to change the value of actual argument in the function being called, you should call the function by reference utilizing pointers in C. Again, when a function is to return more than one value, C pointers are to be used.
5. Arrays and Pointers in C:
An array is simply a non-empty set of sequentially indexed elements of similar kind of data in which each element has a certain identifying number. For example, int num[5] is an array of size 5 in which each of the element is of integer data type. In this array, the objects or the elements are named as num[0], num[1], num[2]…. num[6] in storage area, and there is no padding between array members.
In C arrays, the base address is the location of the first member and the compiler defines the name of the array as a constant pointer to the first element. So, arrays and pointers in C are inseparably related but it doesn’t mean their function is same.
To make a clear concept of memory allocation in array and its relation with pointers in C, lets consider an example of an array defined as float value[3]. If this array, the respective values of array members starting from value[0] to value[4] are 10, 14, 17, and 78. Assuming the base address as 2000 and that each element requires 4 byte of memory for being stored, it can be represented as:
In this example, the array name is defined as a constant element pointing to the first element – value[0], with address 2000. It means, value = &value[0]= 20000. Now, let me define a new variable x as float pointer. The pointer variable x can be now related to the array as follows:
x=value;
This statement is similar to x=&value;
So, simply the concept of index and scale factor can be applied to calculate the address of array element. But, instead of using this concept, you can implement pointers in C to allocate the address of an array element, and it is not required to assign the base address of the array to another pointer variable.
In the above example, *(x+3) indicates the value of element value[3]. Since the programs using C pointers notation are faster than the array notation, it is better to represent the value of the element value[3] as *(value+3).
Since C pointers are variable, you can define arrays of pointer, for example: *a[10]. In this expression, a is an arrays of 10 pointers, all pointing to integer data type.
For a more clear concept of accessing the array element using C pointers, go through the following program:
#include<stdio.h>
main()
{
int num[5], I;
printf(“ Enter the numbers: “);
for(i=o; i<=5; i++)
{
scanf(“%d”,num=i);
}
for(i=0; i<5; i++)
{
printf(“%d”, *(num+1));
}
}
//Run this code and observe the output on your own.//
Comparing C arrays and pointers:
While programming in C using arrays and pointers, you need to know that there is no bound checking of arrays and pointers. You can go beyond the array memory and overwrite the things; however C pointers are different. Pointers in C are variables but arrays can’t be considered as variables. So, for example: if you write – value=x and value++, it is an illegal statement in C. So, the main difference between the C arrays and pointers is Dynamic Memory Allocation in case of pointers while in case of arrays it’s Static Memory Allocation.
6. Pointers to Pointers in C:
In C language, you can use pointer that points to another pointer. For this, you have to add a asterisk sign (*) for each level of reference in their declaration. Observe the following source code to understand this:
char x;
char *y;
char **z;
x=’a’;
y=&x;
z=&y;
Assume random memory location of these variables x, y and z as 1030, 2545 and 5041, which gives us the following information:
- z has the type char** and value 2545
- *z has the type char* and value 1030
- **z has the type char and value ‘a’
7. Void and Null Pointers in C:
In C programming, void signifies the absence of data type; so void pointers are the pointers that point to a value which has no data type. Such pointers have no determined length and dereference properties. Void pointers in C can point to any data type. A integer or float type of void pointer can point to a string data type. But, when it comes to the exchange of data, void pointers have a big limitation: the data pointed by them cannot be easily dereferenced.
On the other hand, null pointers in C are the pointers of any data type which have a special value indicating that they are not pointing to any valid reference or memory or memory address. You should not be confused with null pointers and void pointers. A null pointer points nowhere but a void pointer always points somewhere. This is the basic difference between them.
Try analyzing the source code below to understand more about the use of C void pointers.
#include<stdio.h>
void main()
{
void *a, *b; /* declaration of void Pointers in C*/
int p=77,q=40,r; /* declaration and initialization of ordinary integer variable*/
a=&p, b=&q; /* The initialization of pointer*/
r=*(int*)a+*(int*)b;/* conversion of void pointer to integer*/
printf(“p+q=%d”, r);
}
//Output:
p+q=117//
Here’s one picture I’d like to share:
To Sum it Up:
C pointers are the variables which are defined to store the address of other variable. Pointers are declared by giving type and name: e.g. int *var where int is a data type and var is a pointer variable. The * sign tells the computer that the integer variable var is a pointer.
Reasons why you should use pointers in C:
- You will be able to access a variable which has been defined outside of the function by using pointers.
- When more than one value is to be returned from a function then, C pointers need to be used.
- In order to store the address created by malloc(), calloc, realloc() in Dynamic Memory Allocation, pointers are essential.
- Pointers in C provide efficient way to manipulate data table.
- For the generation of fast and efficient source code, there must be use of pointer.
- During the manipulation of objects such as in sorting, you must use pointers.
Also see,
C Interview Questions and Answers
Pointers are regarded as the beauty of C. So, the first thing you need to learn before using C pointers is the initialization and declaration of pointers with proper syntax and format. I hope this article helped you in one way or another as a beginners guide to learn what pointers are and the implementation of pointers in C. If you have queries and feedback, bring them up to me from the comments section below.