English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C / C ++Pointer

Basically, a pointer is a variable that stores the address of another variable. When we allocate memory for a variable, the pointer points to the address of that variable. The unary operator (*)is used to declare a variable, it returns the address of the allocated memory.

The following is the syntax of a pointer.

datatype *variable_name;

Here,

datatype-The data type of the variable, for example int, char, float, etc.

variable_name-This is the variable name given by the user.

The following is an example of a pointer.

Example

#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 Result

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

In the above program, an integer variable "a" and a pointer variable " * The value and address stored in the pointer variable are as follows:

int a = 8;
int *ptr;
ptr = &a;