C pointer Address operators

There are two important operators which are highly required, if you are working with the pointers. Without these operators, we cannot work with the pointers.

The operators are:

  1. The * Operator (Dereference Operator or Indirection Operator)
  2. The & Operator (Address Of Operator)

1) The * Operator (Dereference Operator or Indirection Operator)

"Dereference Operator" or "Indirection Operator denoted by asterisk character (*), * is a unary operator which performs two operations with the pointer (which is used for two purposes with the pointers).

  1. To declare a pointer
  2. To access the stored value of the memory (location) pointed by the pointer

A) To declare a pointer

Consider the syntax

data_type *pointer_variable_name;

Let suppose, if we declare a pointer ptrX to store the memory address of an integer variable; then the pointer declaration will be int *ptrX;

B) To access the stored value of the memory (location) pointed by the pointer

Consider the syntax

*pointer_variable_name;

Let suppose, if there is a pointer variable ptrX which is pointing to the address of an integer variable x; then to access the value of x, *ptrX will be used.

2) The & Operator (Address Of Operator)

The "Address Of" Operator denoted by the ampersand character (&), & is a unary operator, which returns the address of a variable.

After declaration of a pointer variable, we need to initialize the pointer with the valid memory address; to get the memory address of a variable Address Of" (&) Operator is used.

Consider the program

#include <stdio.h>
int main()
{
	int x=10;	//integer variable
	int *ptrX;	//integer pointer declaration
	ptrX=&x;	//pointer initialization with the address of x
	
	printf("Value of x: %d\n",*ptrX);
	return 0;
}

Output

Value of x: 10

Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.