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