What the Heck is Pointer in C?
While learning C/C++, you may have run over the term ‘Pointers’, and frequently heard that it is an idea difficult to comprehend. Pointers are very useful and powerful concept of C/C++ and, it isn’t that hard to comprehend. This article will introduce you with the real power of pointers in C.
Introduction To Pointer
A pointer is a variable that contains a memory address of another variable. A pointer variable is declared to some types like other variable, so that it will work only with data of given type. Just as integer variable can hold integer data only, pointer variable can point to one specific data type(integer pointer can only hold the address of integer data). Pointer can have any name that is legal to any other variables but it is always preceded by asterisk(*) operator.
Uses of Pointer
- Pointer is used is Dynamic Memory Allocation.
- Pointer can be used to return multiple value from functions.
- Pointer can be used to access the element of array.
- Pointer increase the execution speed of program.
- Pointer are used to create complex data structure.
Pointer Declaration
Syntax:
Data_type *Var;
Example:
int *x;
float *y;
char *z;
In the above example, ‘x’ is an integer pointer which can hold the address of integer data. Similarly, ‘y’ is a float pointer which can hold the address of float data, and ‘z’ is a character pointer which can hold the address of character data.
Understanding With Example:
#include<stdio.h>
#include<stdio.h>
int main(){
int x; //Var Declaration
int *p; //Pointer Declaration
x = 69; //Var Initialization
p = &x; //Pointer Initialization
printf("Address is %d \n",&x);
printf("Address is %d \n",p);
printf("Value is %d \n",*p);
printf("Value is %d",x);
return 0;
}Output:Address is 59084068
Address is 59084068
Value is 69
Value is 69
Let’s Illustrate It
‘x’ is an integer variable and it’s value is 69. The variable p is pointer variable. p = &x assigns address of ‘x’ to ‘p’. ‘p’ is a pointer to ‘x’. We can use ‘p’ to access the address and value of ‘x’.’p’ is noting but variable which store the memory address of ‘x’. *p is used to display the value stored at a memory location pointed by pointer variable ‘p’. OH!!! Crap So Confusing….
Dereference Operator
The dereference operator (*) is a prefix operator that can be used with any pointer variable, like *p. This returns the value of the variable pointed at by that pointer.
Address Operator
The operator ‘&’ is known as address operator. This returns the address of variable. We have already used this in Scanf.
Initializing Pointer
Address of variable can be assigned to pointer variable. Address of pointer can be also stored in another pointer. Let’s see how to initialize pointer.
int *p = &x;ORint *p; int *p = &x;ORint *p;
p = &x;
p = &x;
Bad Pointer
With great power comes great responsibility. Pointers can make your system crash and corrupt memory locations if not handled properly. When a pointer variable is declared, it does not have any value(address) inside it. The pointer is uninitialized or bad pointer or wild pointer. You must assign some address to it. Sometimes it can lead to serious run time error.
Void Pointer
A void pointer is also known as Generic pointer. Void pointer is an special type of pointer which can point to any data type. Typecasting is used to change void to any other data type.
Syntax:
Void *Var=;
Null Pointer
A null pointer is a type of pointer which points to nothing. We can define null pointer using constant NULL. A null pointer always contains value 0
Array of Pointers
Like array of variables, we can create an array of pointers too. An array of pointer can be declared as: type *var[size];
Example
int *ptr[5];
This declare an array of 5 pointers, each points to a address of an integer data. It can be initialized as:
int u=1,v=2,x=10,y=20,z=30int ptr[0] = &u;
int ptr[1] = &v;
int ptr[2] = &w;
int ptr[3] = &x;
int ptr[4] = &y;
Double Pointer
A pointer to a pointer is called a double pointer.
Example
#include<stdio.h>
int main()
{
int num = 69;
int *p1;
int **p2; // double pointer
p1 = #
p2 = &p1;
printf("Address of num = %d\n", p1);
printf("Address of p1 = %d\n", p2);
}
Pointer and Array
Arrays and pointers are highly linked in C/C++. Fully understanding the relationship between the two probably requires several days of study and experimentation, but it is well worth the effort.
Array name itself is an pointer. It points to the address of 0th element of an array. 1th element of array can be expressed as array name + 1 or simply as &array[1]. Similarly 2nd can be expressed as array name + 2 or as &array[2].
Example
#include<stdio.h>
int main()
{
int arr[5] = {1,2,3,4,5};
printf("Addr of 0th is %d \n",arr);
printf("Addr of 1th is %d \n",arr+1);
printf("Addr of 2th is %d \n",arr+2);
printf("Addr of 3th is %d \n",arr+3);
printf("Addr of 5th is %d \n",arr+4);
}
Passing Pointer to Function
A pointer can be passed to a function as an argument. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable.This mechanism is known as Call by Reference or Call by Address.
Example
#include <stdio.h>void swap(int*, int*); //Swap function declarationint main()
{
int x, y;printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);printf("Before Swapping\nx = %d\ny = %d\n", x, y);swap(&x, &y);printf("After Swapping\nx = %d\ny = %d\n", x, y);return 0;
}
void swap(int *a, int *b)
{
int temp; temp = *b;
*b = *a;
*a = temp;
}
Pointer and String
A String is a sequence of characters stored in an array and arrays are highly connected with pointers, we can say strings and pointers are also highly connected to each other.
Example
*char text[10] = "HelloBabe";
As string is a sequence of characters stored in an array, string variable is also a pointer to a first character of the string. Address of first char of string can be accessed as variable name(text). Similarly address of second char can be accessed as array name + 1.
Example Programs
1. C program to change the value of constant integer using pointers.
#include <stdio.h>
int main()
{
const int a=60;
int *p;
p=&a;
printf("Before changing value of a: %d",a);
//assign value using pointer
*p=69;
printf("\nAfter changing value of a: %d",a); return 0;
}
2. C program to demonstrate example of double pointer
#include <stdio.h>
int main()
{
int a;
int *p1;
int **p2;
p1=&a;
p2=&p1;
a=100;
//access the value of a using p1 and p2
printf("\nValue of a (using p1): %d",*p1);
printf("\nValue of a (using p2): %d",**p2);
//change the value of a using p1
*p1=200;
printf("\nValue of a: %d",*p1);
//change the value of a using p2
**p2=200;
printf("\nValue of a: %d",**p2);
return 0;
}
Phew! Yeah, we’re finished. We completed learning basic of pointers. This article is done, but you shouldn’t be done with pointers. Play with them. Best Wishes.