Pointers in C/C++

CC++Server Side Programming

Basically, pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory.

The following is the syntax of pointers.

datatype *variable_name;

Here,

datatype − The datatype of variable like int, char, float etc.

variable_name − This is the name of variable given by user.

The following is an example of pointers.

Example

 Live Demo

#include <stdio.h>
int main () {
   int a = 8;
   int *ptr;
   ptr = &a;
   printf("Value of variable : %d\n", a);
   printf("Address of variable : %d\n", ptr);
   printf("Value pointer variable : %d\n",*ptr);
   return 0;
}

Output

Value of variable : 8
Address of variable : -201313340
Value pointer variable : 8

In the above program, an integer variable ‘a’ and a pointer variable ‘*ptr’ is declared. The variable value and address stored by the pointer variable are shown as follows −

int a = 8;
int *ptr;
ptr = &a;
raja
Published on 06-Nov-2018 11:11:44
Advertisements