C Programming Tutorial – Pointers

Pointer is a difficult concept for beginners of C programming language. Yet, you must master this concept because this is probably the most important concept in C language.

To learn pointers, you must learn – the pointer notations, the pointer syntax, and basic pointer concepts.

What is C pointer?

A normal variable store a value of a specific data type. However, a pointer is a special variable that holds memory address of another variable as a reference.

The main advantages of the pointer are

  • Saves memory
  • Faster Access to variables

Let us briefly explain each of the advantages.

Pointer saves memory

A pointer variable saves memory by creating a reference to a variable that has value, instead of creating a new value and storing it. Creating a reference to another variable is easy and C programming language allow more than one pointers to refer the same variable. This concept is known as “sharing”.

Faster Access to Variables

A pointer is faster that a regular variable when used as parameter to a function. There are two ways to pass parameters to a function – Call-by-value and Call-by-reference.

When a variable is passed as call-by-value method, a copy of the variable is passed as a parameter. This method is slower because it has to copy the variable.

C program for Call-by-Value

The call-by-reference method involves pointers which pass a reference to the original variable. Therefore, using pointer make the program faster.

C program for Call-by-Reference

C Pointer Operators

There are three kinds of operator used while working with pointers. Each of the operators is used in a specific way with pointers as well as other variables.

  • Assignment operator (=): It assigns values to variables.
  • Dereference operator (*): Pointers can access values of variables using this operator.
  • Address-Of operator (&): Unary operator to get access to memory address of a variable.

The operators are part of syntax to define and use pointers.

Syntax for Pointers
Most people do not understand pointers because they do not understand the syntax, what it means to declare pointers, what does it mean to reference a variable.

A visual representation should clear all doubts about the pointer. In this section, you will find syntax and a visual representation of syntax in the memory.
Pointer Declaration
To declare a pointer type variable, you need <data type>, followed by * asterisk symbol and a < user-defined variable name>.

data_type* user_defined_name;

For example

int* p;
float* p;
Pointer Declaration

Pointer Declaration

Note that the asterisk symbol also means a dereferencing operator, so you must be careful in using it. In a different context, it means differently.

Pointer Initialization
A pointer starts out bad, but we need to point it to a variable. Never use a pointer that is not initialized, you will encounter an error. A pointer must always point to something before it is used.

The <user-defined pointer name> is assigned memory address of a target variable, see the figure below.

user_defined_pointer_name = &variable_name;

Example

int num = 1100; /* Variable declaration and initialization is must */
int* p; 
p = #
Pointer Initialization

Pointer Initialization

The above figure shows that pointer variable p is assigned memory address of variable number. The address-of operator before variable num gives the memory address.

De-referencing pointer
De-referencing means getting access to the values of the variable pointer is pointing to. To do this, you need the dereferencing operator (*).

user_defined_variable_name = *pointer_name;
/* must assign the deference value to another variable or print it as output */

For example

int c, d;
c = 100;
int* p;
p = &c;
d = *p; /* now d is assigned the value of 100 using dereference operator */
De-referencing Pointer

De-referencing Pointer

Bad Pointer Concept
Every pointer that you declare starts out bad, but you must assign an address of a proper variable to the pointer. Until then, it remains a bad pointer. If you get errors in your program that use pointers, check for bad pointers first.

Bad Pointer

Bad Pointer

Different programming constructs behave differently to a bad pointer which points to nothing. Some just halt the program, and others crash the program itself like C/C++.

Null Pointer Concept
The NULL has a default value in every compiler which means No Value or sometimes a 0. When the pointer points to NULL then you can say that it points to nothing.

That is the reason, you must never de-reference a NULL pointer because technically, it points to nothing. Sometimes the NULL is assigned a value of 0 or any other constant values. But still, you cannot de-reference the NULL pointer.

NULL Pointers

NULL Pointers

Sharing Concept
You can assign one pointer to another pointer. Basically, this will give the second pointer the address of the original variable. See the figure below. In other words, they both share the same value and points to the same variable.

The assignment is quite straightforward, you assign one pointer to another directly like any other variable.

For example

q = p;
Pointer Sharing

Pointer Sharing

Example Program – Pointers

#include stdio.h

#include stdlib.h

int main()
{

    int num = 100;

    int* p;

    int* q;

    p = #

    q = p;

    printf("\n\n\n\n");

    printf("\t\t\tAddress of num= %u\n",&num);

    printf("\t\t\tAddress of p = %u\n",&p);

    printf("\t\t\tAddress stored at p = %u\n",p);

    printf("\t\t\tAddress of q = %u\n",&q);

    printf("\t\t\tValue of pointer p= %d\n",*p),

    printf("\t\t\tValue of pointer q = %d\n",*q);

    getch();

    return 0;

}

Output – Pointers

The output of the above program is given below. The program shows address of all the pointers: p, q and variable: num. It also use the dereferencing operator to retrieve the values for pointers.

Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.