C Pointer with Examples

by anupmaurya
362 views

A Pointer in C language is a variable which holds the address of another variable of same data type.

Pointers are used to access memory and manipulate the address.

Before we start understanding what pointers are and what they can do, let’s start by understanding what does “Address of a memory location” means?

Address in C

Whenever a variable is defined in C language, a memory location is assigned for it, in which its value will be stored. We can easily check this memory address, using the & symbol. By the way, & is called the reference operator.

If you have a variable var in your program, &var will give you its address in the memory. We have used address numerous times while using the scanf() function.

scanf("%d", &var);
Code language: C/AL (cal)

Here, the value entered by the user is stored in the address of var variable. Let’s take a working example.

#include <stdio.h> int main() { int var = 10; printf("var: %d\n", var); // Notice the use of & before var printf("address of var: %p", &var); return 0; }
Code language: PHP (php)

Output

var: 10 
address of var: 2686778

C Pointers

Pointers (pointer variables) are special variables that are used to store addresses rather than values.

Pointer Syntax

Here is how we can declare pointers.

int* p;

Here, we have declared a pointer p of int type.

You can also declare pointers in these ways.

int *p1; int * p2;

Let’s take another example of declaring pointers.

int* p1, p2;

Here, we have declared a pointer p1 and a normal variable p2.

Assigning addresses to Pointers

Let’s take an example.

int* pc, c; c = 5; pc = &c;
Code language: HTML, XML (xml)

Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.

Get Value of Thing Pointed by Pointers

To get the value of the thing pointed by the pointers, we use the * operator. For example:

int* pc, c; c = 5; pc = &c; printf("%d", *pc); // Output: 5
Code language: JavaScript (javascript)

Here, the address of c is assigned to the pc pointer. To get the value stored in that address, we used *pc.

Note: In the above example, pc is a pointer, not *pc. You cannot and should not do something like *pc = &c;

By the way, * is called the dereference operator (when working with pointers). It operates on a pointer and gives the value stored in that pointer.

Changing Value Pointed by Pointers

Let’s take an example.

int* pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Ouptut: 1
Code language: JavaScript (javascript)

We have assigned the address of c to the pc pointer.

Then, we changed the value of c to 1. Since pc and the address of c is the same, *pc gives us 1.

Let’s take another example.

int* pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Ouptut: 1 printf("%d", c); // Output: 1
Code language: JavaScript (javascript)

We have assigned the address of c to the pc pointer.

Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is the same, c will be equal to 1.

Let’s take one more example.

int* pc, c, d; c = 5; d = -15; pc = &c; printf("%d", *pc); // Output: 5 pc = &d; printf("%d", *pc); // Ouptut: -15
Code language: JavaScript (javascript)

Initially, the address of c is assigned to the pc pointer using pc = &c;. Since c is 5, *pc gives us 5.

Then, the address of d is assigned to the pc pointer using pc = &d;. Since d is -15, *pc gives us -15.

Example: Working of Pointers

Let’s take a working example.

#include <stdio.h> int main() { int* pc, c; c = 22; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 22 pc = &c; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 22 c = 11; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 11 *pc = 2; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 2 return 0; }
Code language: PHP (php)

Output

Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784
Content of pointer pc: 22

Address of pointer pc: 2686784
Content of pointer pc: 11

Address of c: 2686784
Value of c: 2

NULL Pointers

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program

#include <stdio.h> int main () { int *ptr = NULL; printf("The value of ptr is : %x\n", ptr ); return 0; }
Code language: PHP (php)

When the above code is compiled and executed, it produces the following result −

The value of ptr is 0

In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

To check for a null pointer, you can use an ‘if’ statement as follows −

if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

Benefits of using pointers

Below we have listed a few benefits of using pointers:

  1. Pointers are more efficient in handling Arrays and Structures.
  2. Pointers allow references to function and thereby helps in passing of function as arguments to other functions.
  3. It reduces length of the program and its execution time as well.
  4. It allows C language to support Dynamic Memory management.

IMPORTANT POINTS REMEMBER ABOUT POINTERS IN C

  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always be a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for 16 bit compiler).

You may also like