Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Pointers in C

Pointers are a fundamental concept in C. Most people get confused while learning it but if you practice and have patience you will learn easily. Pointers are used to access the memory locations of data items. By using pointers, we can get the address where a variable is stored in memory or we can get the starting address of an array i.e. the address of its first element. Like an ordinary variable stores a value, a pointer variable stores a memory address. In C, memory addresses are in hexadecimal.

Declaration of pointers

To declare a pointer variable we use the asterisk (*) symbol. For example -

    int *p ;         // p is a pointer that can store the address of an integer.
                

Using pointers

Suppose that you have a variable. If you want a pointer that stores the memory address of this variable you can write -

    #include <stdio.h>
    void main()
    {

    int a = 5 ;
    int *p ;

    p = &a;        

    int b = *p;     // This is called dereferncing.

    printf("Value of a = %d \n",a);
    printf("Memory address of a = %p \n",p);
    printf("Value present on memory address stored by p = %d \n",b);

    }
        

Output

    Value of a = 5 
    Memory address of a = 0x7ffe5d8b7bfc 
    Value present on memory address stored by p = 5 
    
The address of (&) operator is used to get the memory address where a is stored. This address is stored in the pointer p. The %p format specifier is used to print the hexadecimal address value.
Now, p contains the memory address of variable a. We can get the value stored at a memory address by using the asterick(*) operator. This is called dereferencing.The variable b contains the value stored at the memory location present in p i.e. b contains the value of a.

Pointer Arithmetic

Pointers support arithmetic operations such as addition and subtraction. However, it does not support multiplication and division.
Suppose that p is a pointer that contains the memory address of a(variable). Suppose that the memory address of a is 1000. Then if we decrement the value of p, it will give us 996, and if we increment the value of p, it will give 1004. This is because p is an integer pointer i.e it stores the address of an integer variable. The size of integer is 4 bytes. If we increment the value of p, it gives 1004 because it is the next valid address since bytes 1000 to 1003 are occupied for storing a. Similarly, in the case of decrement. If p is of char data type, then it would give 1002 on increment and 998 on decrement because char takes 2 bytes of memory.
Consider the following example -

    #include <stdio.h>
    void main()
    {

    int a = 5 ;
    int *p = &a ;         

    printf("Memory address stored at p = %p \n",p);
    p++ ;

    printf("Memory address stored at p = %p \n",p);
    p -= 2 ;

    printf("Memory address stored at p = %p",p);

    }
        

Output

    Memory address stored at p = 0x7fff607f484c 
    Memory address stored at p = 0x7fff607f4850 
    Memory address stored at p = 0x7fff607f4848