Pointer - C Tutorial

Pointer

A Pointer is a variable that holds address of another variable of same data type.

Pointer is Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”.

Lets try to understand the concept.

As shown in the above diagram:

  • A normal variable ‘num’ has a memory address of EEE3 and holds a value 50.
  • A pointer variable has its own address CCC4 but stores EEE3, which is the address of the variable ‘num’

& Operator – “Address of” Operator

An address-of operator is a mechanism within C that returns the memory address of a variable. These addresses returned by the address-of operator are known as pointers, because they "point" to the variable in memory.

The address-of operator is a unary operator represented by an ampersand (&). It is also known as an address operator.

* Operator – “Value at Address” Operator

The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator. It is also used for declaring pointer variable.

Consider the below code:


int main()
{
   int num = 50;
   printf("Value of num is: %d\n", num);
   printf("Address of num is: %u", &num);
   return 0;
}

Output:

    Value of num is: 50 
    Address of num is: 64420