Signup/Sign In

Using Pointers in C

In this tutorial, we will learn how to declare, initialize and use a pointer in C language.

Before you continue, check these topics out:

A pointer is a variable used to store memory address. Hence, we have to declare and initialise(assign it a value) it just like any other variable. Pointers can be very useful in some use-cases, like:

  • They make accessing array elements easier.
  • We can implement linked lists, trees, and graphs using pointers.
  • We use pointers for accessing dynamically allocated memory.
  • We can return more than one value from a function using pointers.

So let's see how we can create pointers, assign values to pointers, perform pointer coversions, pointer arithmetic and pointer comparisons.

Declaring a Pointer in C

The general syntax of pointer declaration is,

type *pointer_name;

Here, pointer_name is the name of the pointer and that should be a valid C identifier.

The datatype of the pointer and the variable to which the pointer variable is pointing must be the same.

Following are some examples of declaring a pointer in C:

int *ptr;     //pointer to int
float *ptr;   //pointer to float
char *ptr;    //pointer to char
double *ptr;  //pointer to double

Just like a variable, pointer is declared in the same way, just with an additional pointer operator *.

Assigning Value to a Pointer Variable

When we declare a pointer, it contains garbage value, which means it could be pointing anywhere in the memory. Pointer Initialization is the process of assigning the address of a variable to a pointer. In C language, the address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.

For example,

#include<stdio.h>

int main(void)
{
    int a = 10;
    // declare a pointer
    int *ptr;
    // assign value to pointer
    ptr = &a;

    printf("Value at ptr is: %d \n", *ptr);

    printf("Address pointed by ptr is: %p \n", ptr);

    return 0;
}


Value at ptr is: 10
Address pointed by ptr is: 0x7fff99c0e6c4

Run Code →

initialize pointer variable in C

Pointer variables must always point to variables of the same datatype.

For example, if we have a float type variable and an int type pointer, then C compiler will give error.

#include<stdio.h>

int main(void)
{
    float a = 10;
    // declare a pointer
    int *ptr;
    // assign value to pointer
    ptr = &a;

    printf("Value at ptr is: %d \n", *ptr);

    printf("Address pointed by ptr is: %p \n", ptr);

    return 0;
}


warning: assignment from incompatible pointer type
ptr = &x; ^

Run Code →

While declaring a pointer variable, if it is not assigned to anything then it contains garbage value. And that can lead to unexpected errors in your program. Hence, it is recommended to assign a NULL value to it.

When we assign NULL to a pointer, it means that it does not point to any valid address. NULL denotes the value 'zero'.

set pointer variable value

A pointer that is assigned a NULL value is called a NULL pointer in C.

We can give a pointer a null value by assigning it zero to it. For example,

 int *ptr = 0;

The above code will initialize the ptr pointer will a null value.

We can also use the NULL macro, which is nothing but a predefined constant for null pointer. This is defined in the <stdio.h> header library.

 int *ptr = NULL;

Pointer to Pointer Assignment in C

We can use an assignment operator to assign value of a pointer to another pointer variable. But for such assignment, types of both the pointer should be same.

Let's take a code example,

#include<stdio.h>

int main(void)
{
    float a = 10;
    // declare two pointers
    int *p1, *p2;

    // assign value to pointer
    p1 = &a;
    // assign value from one pointer to another
    p2 = p1;

    printf("Value at p1 and p2: %d %d \n", *p1, *p2);

    printf("Address pointed by p1 and p2: %p %p \n", p1, p2);

    return 0;
}


Value at p1 and p2: 10 10
Address pointed by p1 and p2: 0x7fff99c0e6c4 0x7fff99c0e6c4

As you can see in the code example above, multiple pointers can point to the same variable but they should be of the same data type.


Pointer Type Conversion in C

We can assign a pointer of one type to a pointer of another type by doing pointer type conversion.

1. void * pointer

The pointers of type void * are known as Generic pointers, and they can be assigned to any other type of pointer. Also, any other type of pointer can be assigned to a void * pointer.

2. Pointer type conversion

For pointer type other than void *, we have to explicitly cast pointer from one type to another. But this can lead to unexpected behaviour for incompatible datatypes.

For example, if we have a variable of type double, and we want to use a pointer of type int to point to this variable. Even after using explicit cast, the pointer will work as if it is pointing to a int type value. Now, a double type is of 8 bytes whereas an int type is of 4 bytes, hence, 4 bytes of information will be lost.

Let's see how we can use explicit cast for pointer conversion.

#include<stdio.h>

int main(void)
{
    double a = 1000.10;
    double b;
    // declare a pointer
    int *p1;
    // assign value to pointer with casting
    p1 = (int *) &a;

    // value of 'b' should be same as 'a', but it won't be
    b = *p1;

    printf("Value of a is: %f \n", b);

    return 0;
}


Value of a is: -858993459.000000

Run Code →


Pointer and Arrays


Derefrencing Pointer in C

Once a pointer has been assigned the address of a variable, the pointer is dereferenced, using the indirection operator or dereferencing operator, which is a *, to access the value of the variable.

For example, if we have,

int a  = 5;
int *ptr1 = &a;
float b = 5.5;
float *ptr2 = &b;
// *ptr1 = 2 is equivalent to a = 2
// (*ptr1)++ is equivalent to a++
// float z = *ptr2 + 4.2 is equivalent to float z = b + 4.2;

Here is one complete program,

#include <stdio.h>

int main()
{
    int a;  
    a = 10;
    int *p = &a;     // declaring and initializing the pointer

    //prints the value of 'a'
    printf("%d\n", *p);  
    printf("%d\n", *&a);  
    //prints the address of 'a'
    printf("%u\n", &a);    
    printf("%u\n", p);     
    
    printf("%u\n", &p);    //prints address of 'p'
    
    return 0;
}


10
10
3795480300
3795480300
3795480304

Points to Remember while using Pointers

  • A pointer variable stores the address of a variable. We use * to declare and identify a pointer.

  • We can find the address of any variable using the & (ampersand) operator.

  • The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable of int type.

  • We can dereference a pointer variable using a * operator. Here, the * can be read as 'value at'.

Since you have now learned the basics of Pointers in C, you can check out some C Pointer Programs where pointers are used for different use-cases.

Read More: