C programming pointers


In this tutorial, you will learn about c programming pointers, one of the powerful features of C.

c programming pointers

Pointers in C programming

Pointer is a striking and most powerful feature of C programming. As the name itself suggests a pointer is something that points something.

It can also be described as a variable that points to an ordinary variable of any type by holding the address of that variable.

For the better understanding of C programming pointers consider following declaration:

int x = 1;

Above declaration reserves a memory block with name x and its value 1.

Assume that x is located at address 3000.

Therefore: &x = address of x = 3000

To print the value and the address the program would be

#include <stdio.h>
int main()
{
    int x = 1;
    printf("Value of x = %d",x);
    printf("Address of x = %d",&x);
    return 0;
}

Output

Value of x = 1
Address of x = 3000

In above program, the address of x can be assigned to another variable p as shown below:

int x = 1;
int *p; // pointer variable declaration
p = &x;

c pointer

So the pointer variable is declared as:

data_type *variable_name;
Good Programming Practice
It is a good idea to include "ptr" in pointer variable name to distinguish easily.

How to access the value of a variable using pointer variable?

So far we know that pointer variable stores the address of the variable. However, the value of the variable can also be accessed by pointer variable using the syntax:

data_type *variable_name ;
Common Programming Error
Pointer needs to be handled carefully. If the pointer is not initialized properly dereferencing it cause an error.

For example, if p is the pointer variable that points to the address of the variable x then *p gives the value of variable x. The following program will clarify the concept of C programming pointers.

#include <stdio.h>
int main()
{
    int x, y, z, *p1, *p2;
    x = 2;
    y = 3;
    p1 = &x;
    p2 = &y;
    z = *p1 + *p2;
    printf("sum = %d",z);
    return 0;
}

Output

sum = 5

Explanation

In the above program, pointers p1 and p2 point to the variable x and y. Dereference operator is used for extracting the value from that memory location.

Reference operator (&) and Dereference operator (*)


The refrence operator & returns the address of the variable.

The dereference operator * or indirection operator returns the value from the address.

Example to demonstrate the use of & and * pointer operators.

#include <stdio.h>
int main()
{
  int x;     //integer variable
  int *y;    //y is pointer to an integer
  
  x = 5;
  y = &x;    //pointing to address of x

  printf("The address of x = %p", &x);
  printf("\nThe value of y = %p", y);

  printf("\n\nThe value of x = %d", x);
  printf("\nThe value of *y = %d", *y);

  printf("\n\n* and & are complement of each other"
         "\n&*y = %p"
         "\n*&y = %p\n", &*y, *&y );
  return 0;
}

Output


The address of x = 0060FF0C
The value of y = 0060FF0C

The value of x = 5
The value of *y = 5

* and & are complement of each other
&*y = 0060FF0C
*&y = 0060FF0C

%p outputs the memory location in hexadecimal.

The value of y and address of x are same in the output which confirms that the address of x is assigned to pointer variable y.

printf("\n\n* and & are complement of each other"
         "\n&*y = %p"
         "\n*&y = %p\n", &*y, *&y );

This proves that & and * are complements of each other.