Easy Tutorial
❮ C Function Mktime C Examples Swapping Cyclic Order ❯

C Pointers

Learning pointers in C is both simple and fun. Pointers can simplify the execution of some C programming tasks and enable other tasks, such as dynamic memory allocation, which are impossible without them. Therefore, to become an excellent C programmer, learning pointers is essential.

As you know, every variable has a memory location, and each memory location defines an address that can be accessed using the & operator, which represents an address in memory.

Consider the following example, which will output the defined variable's address:

Example

#include <stdio.h>

int main ()
{
    int var_tutorialpro = 10;
    int *p;              // Define pointer variable
    p = &var_tutorialpro;

    printf("Address of var_tutorialpro variable: %p\n", p);
    return 0;
}

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

Address of var_tutorialpro variable: 0x7ffeeaae08d8

Through the above example, we have learned what a memory address is and how to access it. Next, let's see what a pointer is.

What is a Pointer?

A pointer is a memory address, and a pointer variable is used to store a memory address. Like other variables or constants, you must declare a pointer before using it to store the address of another variable. The general form of a pointer variable declaration is:

type *var_name;

Here, type is the base type of the pointer, which must be a valid C data type, and var_name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement, the asterisk is used to specify that the variable is a pointer. The following are valid pointer declarations:

int    *ip;    /* A pointer to an integer */
double *dp;    /* A pointer to a double */
float  *fp;    /* A pointer to a float */
char   *ch;    /* A pointer to a character */

All actual data types, whether integer, float, character, or others, have the same type of value for the pointer, which is a long hexadecimal number representing a memory address.

The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to Use Pointers?

Several operations are frequently performed when using pointers: defining a pointer variable, assigning the address of a variable to a pointer, and accessing the value at the address available in the pointer variable. These operations are performed using the unary operator *, which returns the value of the variable located at the address specified by its operand. The following example involves these operations:

Example

#include <stdio.h>

int main ()
{
    int var = 20;   /* Actual variable declaration */
    int *ip;        /* Pointer variable declaration */

    ip = &var;  /* Store address of var in pointer variable */

    printf("Address of var variable: %p\n", &var);

    /* Address stored in pointer variable */
    printf("Address stored in ip variable: %p\n", ip);

    /* Access the value using the pointer */
    printf("Value of *ip variable: %d\n", *ip);

    return 0;
}

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

Address of var variable: 0x7ffeeef168d8
Address stored in ip variable: 0x7ffeeef168d8
Value of *ip variable: 20

NULL Pointers in C

It is a good programming practice to assign a NULL value to a pointer variable when there is no exact address to be assigned. A pointer assigned NULL is called a null pointer.

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

Example

#include <stdio.h>

int main ()
{
    int *ptr = NULL;

    printf("The address of ptr is %p\n", ptr);

    return 0;
}

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

The address of ptr is 0x0

On most 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 indicates that the pointer is not intended to point to an accessible memory location. By convention, if a pointer contains the null value (zero), it is assumed to point to nothing.

To check for a null pointer, you can use an if statement as follows:

if(ptr)     /* Executes if ptr is not null */
if(!ptr)    /* Executes if ptr is null */

Detailed Explanation of C Pointers

In C, there are several concepts related to pointers that are simple but important. The following table lists some of the important concepts that a C programmer must be aware of:

Concept Description
Pointer Arithmetic Pointers can be subjected to arithmetic operations: ++, --, +, -
Array of Pointers Arrays can be defined to store pointers.
Pointer to Pointer C allows pointers to pointers.
Passing Pointers to Functions Passing parameters by reference or address allows the passed parameters to be changed in the calling function.
Returning Pointers from Functions C allows functions to return pointers to local variables, static variables, and dynamically allocated memory.
❮ C Function Mktime C Examples Swapping Cyclic Order ❯