Pointers in C programming
Pointers in C programming with examples
Pointers in C are a variable that stores the address of another variable. Every variable is a memory location and it has its address defined which can be accessed using the ampersand (&) operator. ‘&’ denotes an address in memory. Variable can be of type int, char, array, or any other pointer.
Define a Pointer in c
int n = 5; int* p = &n;
Declaring pointers in C programming
The * operator called Dereference operator followed by the name of the pointer gives the value stored in the address pointed by the pointer. It is also called an indirection pointer used to dereference a pointer.
Syntax
int *a;//pointer to int char *c;//pointer to char
Program of declaring a pointer in C
#include<stdio.h> int main(){ int number=50; int *p; p=&number; printf("Address of p variable is %x \n",p); printf("Value of p variable is %d \n",*p); return 0; }
Output
Address of number variable is fff4 Address of p variable is fff4 Value of p variable is 50
NULL Pointer in C
A pointer that is assigned NULL has known a null pointer. A null pointer does not point to an object or a function.
Program of the null pointer in C
#include <stdio.h> int main () { int *ptr = NULL; printf("The value of ptr is : %x\n", ptr ); return 0; }
Output
The value of ptr is 0