Advanced C Pointer Programming chapter 1: Introduction to pointers

Pointers in C is a very interesting topic if understand correctly. Hence in this series of Advanced C Pointer Programming tutorial I shall make you understand pointer in a simple and easy.

The only prerequisite is that you should be knowing the basic syntax of C programming. In each chapter beginning I shall be giving you basic example of the topic and later proceed deep into the topic.

1. what is a Pointer:

A pointer is a variable that holds the address of another variable.

2. Declaring a pointer:

Syntax for declaring a pointer:

data_type *pointer_variabl_name;
Example:
int *ptr;

int* prt;

int * ptr;

From the above declaration we can infer following conclusion:
1. Asterisk is used to declare a pointer.
2. ptr is an integer pointer, hence integer variable address should be assigned to it.
3. As we have not initialized anything, it will have a garbage value.

As long as there is a * operator after the data-type it is considered as a pointer. The space is the preference of the programmer. Usually we follow the first notation.
Note:
Asterisk ‘*’ is used for 3 different types:
1. Used to declare a pointer.
2. Used to dereference a pointer [get the value that the pointer holds].
3. Used for multiplication.
Hence it can be known as an overloaded operator in C.

3. Reading a pointer:

Pointers if read wrongly will give a different meaning. Hence reading a pointer correctly will clear out any confusion even in the most complicated syntaxes.
So here is the point, while reading a pointer you have to read it reverse, i.e from right to left.
Example:
int *ptr => ptr is a pointer variable to an integer

const int *ptr => ptr is a pointer variable to a constant integer

int * const ptr => ptr is a constant pointer variable to an integer

const int * const ptr => prt is a constant pointer to a constant integer

4. The address operator:

As a pointer stores the address of a variable, how do we get the address of that variable ? In C we have once more overloaded operator, ampersand ‘&’ to get the address of a variable.

Example:

int num = 100;

int *ptr = #

Hence the above statement will get the address of “num” variable and stored it in “prt” variable.

5. Dereferencing a pointer with indirection operator:

The process of obtaining the value pointed by the address is called as dereferencing a pointer. In C we have ‘*’ operator to do the dereferencing. This operator is called as a indirection operator.

Point to be taken, only while dereferencing we call it as a indirection operator.

Example:

int num = 10;
int *ptr = #

printf("The value is = %d", *ptr);

The output will be "The value is = 10".

Here the “*ptr” will be converted into *(address_stored). Hence it will go to that address and get the value stored in that address.

You can also change the value of the original “num” variable by using pointer as shown below:

*ptr = 225;

Hence if you check the value of variable “num”, it will return 225.

Below program to illustrate the same.

6. Displaying the address and value that a pointer holds:

WKT ‘&’ is an address operator and is used to display the address.
‘*’ is a pointer variable and also used to dereference to get the value from the address given.

Below program shows the different types of accessing values.

#include <stdio.h>
int main()
{
   int *ptr;
   int num = 20;
   
   printf("Address of num: %p\n", &num);
   printf("Value of num: %d\n\n", num); 
   
   ptr = &num;
   printf("Address of pointer ptr: %p\n", &ptr);
   printf("Address holding by the pointer ptr: %p\n", ptr);
   printf("Value of the pointer ptr: %d\n\n", *ptr);
   

   return 0;
}
Address of num: 0x7ffee38dda2c
Value of num: 20

Address of pointer ptr: 0x7ffee38dda30
Address holding by the pointer ptr: 0x7ffee38dda2c
Value of the pointer ptr: 20

7. Null Pointers:

When pointers are declared and not initialized, they will have a garbage value [random address]. Hence when we try to dereference it, application might get crashed.

Hence it is advisable to initialize a pointer once it is declared. But sometimes it happens that a pointer declared might not be used immediately. At that point it is advisable that a pointer to be initialized to NULL as shown below.

int *ptr = NULL;

Hence when a NULL is assigned to a pointer it means pointer does not point to anything.

In many C libraries the NULL has been defined as:

#define 	NULL 		((void *)0)

An integer 0 casted to “void”.

We can assign ‘0’ instead of NULL to a pointer, but it is not advisable. Most of the programmer use NULL to explicitly say that they are working with pointer.

If we assign ‘0’ instead of NULL, we might get into following issue:

int num = 34;

int *ptr = 0; // Initializes the pointer to NULL.

ptr = &num;

ptr = 0; // here 0 is refered as integet 0 not as NULL.

Two NULL pointers are always equall to each other. But a NULL pointer and UnInitialized pointer are different.

8. Void Pointers:

Void means nothing. If a pointer is of void type, it can hold reference to any data-type.

Example:

void *ptr;

Internally void pointer will be converted to character pointer.

Any pointer can be assigned to void pointer and can be casted back to it’s original datatype.

9. Size of Pointers:

The size of a pointer will remain the same irrespective of the type of pointer. An integer pointer size will be same as a char pointer size. The difference will come when we are dereferencing the pointers.

While dereferencing an integer pointer it will dereference by 4 bytes, while dereferencing an char pointer it will dereference by 1 byte.

Example to show pointer size:

#include <stdio.h>
int main()
{
   int *ptr;
   void *v_ptr;
   
   printf("size of int ptr = %lu\n", sizeof(ptr));
   printf("size of void ptr = %lu\n", sizeof(v_ptr));


   return 0;
}

Output

size of int ptr = 8
size of void ptr = 8

10. Pointer constant and constant to pointer

Pointer to a constant value:

In this type, if a pointer is pointing to some address, the value inside that address cannot be changed.

Syntax for pointer to a constant:

const <data_type> * <pointer_name>

or

<data_type> const * <pointer_name>

Example:

const int *ptr;

int const *ptr;

Example 1:

#include <stdio.h>
int main()
{
   int a = 20;

   int const *ptr = &a;  
  
   *(ptr) = 30;

   return 0;
}

Output:

error: read-only variable is not
      assignable
   *(ptr) = 30;
   ~~~~~~ ^
1 error generated.

Constant address to a pointer (constant pointer):

In this type, the address of the pointer is constant.
It means that if a constant pointer is pointing to some variable, then it cannot point to any other variable.

Syntax of constant pointer:

<data_type> *const <pointer_name>

Example:

int *const ptr;

Example 1:

#include <stdio.h>
int main()
{
   int * const ptr = NULL;
   int a = 20;
   
   ptr = &a;

   return 0;
}

Output:

 error: cannot assign to variable 'ptr'
      with const-qualified type 'int *const'
   ptr = &a;
   ~~~ ^

This is because, you need to assign the value of constant pointer when you declare it.

First we have declared constant pointer to point to NULL value.
Then we are changing the address of the pointer to point to variable “a”. According to constant pointer, it cannot change the address it is pointing to, hence the error.

So how to solve?

We can solve it by assigning the variable address when we declare a pointer.

Example 2:

#include <stdio.h>
int main()
{
   int a = 20;

   int * const ptr = &a;  
   
   return 0;
}

Constant pointer to a constant value

In this type, both the address pointed by the pointer and the value in that address cannot be changed.

Syntax:

const <data_type> *const <pointer_name>;

Example:

const int *const ptr;

Example:

#include <stdio.h>
int main()
{
   int a = 20;

   int const * const ptr = &a;  
  
   return 0;
}
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *