C PROGRAMMING: POINTERS-2

C PROGRAMMING: POINTERS-2

Pointers in Detail

Pointers have many but easy concepts and they are very important to C programming. The following important pointer concepts should be clear to any C programmer −

Sr.No.Concept & Description
1Pointer arithmetic: There are four arithmetic operators that can be used in pointers: ++, –, +, –
2Array of pointers: You can define arrays to hold a number of pointers.
3Pointer to pointer: C allows you to have a pointer on a pointer and so on.
4Passing pointers to functions in C: Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.
5Return pointer from functions in C: C allows a function to return a pointer to the local variable, static variable and dynamically allocated memory as well.

pointer arithmetic:

There are four arithmetic operators that can be used on pointer:

  • ++
  • +

program on pointer arithmetic:

program-1

#include <stdio.h>            
void main(){            
int number=50;        
int *p;//pointer to int      
p=&number;//stores the address of number variable        
printf("Address of p variable is %u n",p);        
p=p-1;       
printf("After decrement: Address of p variable is %u n",p); // P will now point to the immidiate previous location.         
}      

output:

Address of p variable is 3214864300 
After decrement: Address of p variable is 3214864296

program-2

#include<stdio.h>  
int main(){  
int number=50;        
int *p;//pointer to int      
p=&number;//stores the address of number variable        
printf("Address of p variable is %u n",p);        
p=p+3;   //adding 3 to pointer variable    
printf("After adding 3: Address of p variable is %u n",p);       
return 0;  
}    

output:

Address of p variable is 3214864300 
After adding 3: Address of p variable is 3214864312

arrays and pointers:

  • consider the following declaration: int arr[10];
  • What is the type of array?
  • Ans- Integer array
  • However, arr by itself, without any index subscripting, can be assigned to an integer pointer, as shown: int*ptr = arr; (this is a valid statement)
  • After writing this statement if we print: &arr[0] or ptr (both are the same by default).
  • What type does arr[i] have?
  • Ans- integer(int)
  • But if we ask why we used bracket in it? then it is because: arr[i] = *(arr+i) (both are same)
  • arr[i] Using pointer arithmetic:
  • arr[i] == *(arr+i)
  • If arr is a pointer to arr[0] then ( arr+i ) is a pointer to arr[i].

difference Between Arrays and Pointer:

  • An array is a collection of elements of similar data type whereas the pointer is a variable that stores the address of another variable. 
  • An array size decides the number of variables it can store whereas; a pointer variable can store the address of only one variable in it.
  • Arrays can be initialized at the definition, while pointers cannot be initialized at the definition.
  • Arrays are static in nature which means once the size of the array is declared, it cannot be resized according to users requirements. Whereas pointers are dynamic in nature, which means the memory allocated can be resized later at any point in time.
  • Arrays are allocated at compile time while pointers are allocated at runtime.

2- array of pointer:

Just like we can declare an array of intfloat or char etc, we can also declare an array of the pointer, here is the syntax to do the same.

Syntax:
datatype *array_name[size];

Let’s take an example:

1 int *arr[5];

Here arr is an array of 5 integer pointer. It means that this array can hold the address of 5 integer variables. In other words, you can assign 5 pointer variables of type pointer to int to the elements of this array. Let’s make it more clear through coding:

#include<stdio.h>
#define SIZE 10
 
int main()
{
    int *arr[3];
    int a = 10, b = 20, c = 50, i;
 
    arr[0] = &a;
    arr[1] = &b;
    arr[2] = &c;
 
    for(i = 0; i < 3; i++)
    {
        printf("Address = %dt Value = %dn", arr[i], *arr[i]);
    }
 
    return 0;
}

output:

Address = 387130656      Value = 10
Address = 387130660      Value = 20
Address = 387130664      Value = 50

Pointer to Pointer (Double Pointer) :

As we know that, a pointer is used to store the address of a variable in C. Pointer reduces the access time of a variable. However, In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The pointer is used to store the address of other variables of a similar data type. But if you want to store the address of a pointer variable, then you again need a pointer to store it. Thus, when one pointer variable stores the address of another pointer variable.  The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.

syntax:

int **pr;
//Here, we have used two indirection operator(*) which stores and points to the address of a pointer variable i.e, int *

program on pointer to pointer:

#include <stdio.h>

int main() {

    int  a = 10;
    int  *p1;       //this can store the address of variable a
    int  **p2; 
    /*
        this can store the address of pointer variable p1 only. 
        It cannot store the address of variable 'a' 
    */

    p1 = &a;
    p2 = &p1;

    printf("Address of a = %un", &a);
    printf("Address of p1 = %un", &p1);
    printf("Address of p2 = %unn", &p2);

    // below print statement will give the address of 'a'
    printf("Value at the address stored by p2 = %un", *p2);
    
    printf("Value at the address stored by p1 = %dnn", *p1);

    printf("Value of **p2 = %dn", **p2); //read this *(*p2)

    /*
        This is not allowed, it will give a compile time error-
        p2 = &a;
        printf("%u", p2);
    */
    return 0;
}

output:

Address of a = 2686724 
Address of p1 = 2686728 
Address of p2 = 2686732 
Value at the address stored by p2 = 2686724 
Value at the address stored by p1 = 10 
Value of **p2 = 10

THERE IS NO NEED FOR THE BEGINNERS TO GO IN DEEP OF ALL THESE CONCEPTS.

JUST KEEP IN KNOWLEDGE THAT, WHAT ARE THESE POINTERS THAT’S ALL

SO THERE IS NO NEED TO WORRY ABOUT IT.

explanation of the above program:

  • p1 pointer variable can only hold the address of the variable a (i.e Number of indirection operator(*)-1 variable). Similarly, p2 the variable can only hold the address of the variable p1. It cannot hold the address of the variable a.
  • *p2 gives us the value at an address stored by the p2 pointer. p2 stores the address of p1 pointer and value at the address of p1 is the address of the variable a. Thus, *p2 prints the address of a.
  • **p2 can be read as *(*p2). Hence, it gives us the value stored at the address *p2. From the above statement, you know *p2 means the address of variable a. Hence, the value at the address *p2 is 10. Thus, **p2 prints 10.

pointer to function:

Just like any other argument, pointers can also be passed to a function as an argument. we can also create a pointer pointing to a function. The code of a function always resides in memory, which means that the function has some address. We can get the address of memory by using the function pointer.

syntax:

return type (*ptr_name)(type1, type2…);  


example:

int (*ip) (int);  
In the above declaration, *ip is a pointer that points to a function which returns an int value and accepts an integer value as an argument.

program:

#include <stdio.h>
void salaryhike(int  *var, int b)
{
    *var = *var+b;
}
int main()
{
    int salary=0, bonus=0;
    printf("Enter the employee current salary:"); 
    scanf("%d", &salary);
    printf("Enter bonus:");
    scanf("%d", &bonus);
    salaryhike(&salary, bonus);
    printf("Final salary: %d", salary);
    return 0;
}

output:

Enter the employee current salary:10000 
Enter bonus:2000 
Final salary: 12000

AGAIN I WANNA REMIND YOU THERE IS NO NEED FOR BEGINNERS TO GO IN DEEP OF ALL THESE CONCEPTS.

I hope everythimg is clear to you, 
In the next article we will dicuss about "FUNCTION ARGUMENTS" and "CALL TYPE" I will see you next time... 
Also visit our previos articles listed below:

AND MANY MORE…

Scroll to Top