C Pointers

In this article, we will learn about C pointers. Pointers are very useful and powerful features of C programming language as well as C++ programming language. Pointers allow you to work and play with the memory addresses. Let’s learn how to implement pointers in the C program and solve complex problems easily with examples.

Memory Address

As I just said that, pointers work with memory addresses. So before getting into pointers, you should know what are memory addresses in C. In the last article, we have discussed what are memory addresses. If you haven’t read, read it first.

C Pointers

Pointers are special variables in C programming. So you can call it pointer variables. Pointers are used to store memory addresses rather than value.

Declaring Pointers in C

C allows several ways to declare pointers. Here you can follow these ways;

int* ptr; //way 1.
int *ptr; //way 2.
int * ptr; //way 3.
int *ptr1, *ptr2; //multiple pointer declaration.

/* Don't be confused */
int * ptr1, ptr2; //ptr1 is pointer but ptr2 is normal variable.

Here all the declarations mean the same. You can use any one of them. We have declared a pointer variable called ptr. And its type is int or integer. Means, ptr is the memory address reserved for integer data only.

Assigning addresses to pointers

As we already know, pointers work with memory addresses. So in a pointer variable, you can assign addresses rather than value. Here is an example;

int* ptr;
int var;
var = 9;
ptr = &var;

Here, 9 is assigned to var normal variable. and address of var assigned to ptr pointer variable (special variable). 9 is a value. But &var is an address in the memory.

Getting values pointed by pointers

A memory address can store certain types of data. For example, an integer type of pointer store some memory address. And the address can hold integer data. So to extract the value pointed by pointers, we can use the * operator before the pointer. Here is an example;

int* ptr, x;
x = 12;
ptr = &x;

//getting value of x through ptr
printf("%d", *ptr); // Output 12

Now you should be clear that;

  • int *ptr; used only pointer declaration.
  • *ptr is not a pointer,  but ptr is a pointer.
  • *ptr used to extract the data/value assigned in the address of ptr

Changing values

If you have a normal variable and the address of the variable is assigned to a pointer variable, then if you change the value of the variable, it will be applied for all. Let’s take an example;

#include <stdio.h>
int main() {
    int* ptr, x;
    x = 6;
    ptr = &x;
    x = 3;
    printf("%d", x); //Output: 3
    printf("%d", *ptr); //Output: 3
  return 0;
}

Notice that, after reassigning the value of x the pointer value also has been changed. It happened because the ptr pointer is referring to the same address of x.

We can also change the value by the following way;

#include <stdio.h>
int main() {
    int* ptr, x;
    x = 6;
    ptr = &x;
    *ptr = 3;
    printf("%d", x); //Output: 3
    printf("%d", *ptr); //Output: 3
  return 0;
}

Here we changed the value to 3.