Pointer in C
Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr
Symbol | Description |
& (ampersand sign) | Address of an operator is used to contain the address of a variable. |
* (asterisk sign) | Indirection operator is used to accesses the value at the address.. |
- It is used to reduce the code and performance.
- It accesses the direct address of the variable.
- It is used to return multiple values from the function.
- It helps us to access any memory location from the pointer.
int *ptr; char *ch;Let us consider an example:
#include<stdio.h> int main () { int n=100; int *ptr; // pointer variable printf("Address of num variable: %x\n", &n); printf("Address stored in ptr variable: %x\n", ptr ); // address stored in pointer variable printf("Value of *ptr variable: %d\n", *ptr ); //access the value using the pointer return 0; }Output
Address of num variable: f64b7adc Address stored in ptr variable: f64b7bd0 Value of *ptr variable: 1
Null Pointer
A pointer does not assign any value that is known as a NULL pointer. We can create a null pointer at the time of declaration.