Monday, 3 June 2019

What is Pointers in C & how it works?

A pointer is a variable in the C programming language which holds the address of another variable of the same data type. For example, an integer variable contains an integer value, however, an integer pointer holds the address of integer variable. Pointers in C have derived data types. Whenever a variable is declared or initialized in the C programming language, memory is allocated to that variable which stores the value of that variable. We can easily see the address of that variable by using the symbol '&'.


With the help of unary operator & (ampersand), we can access the address of the variable.

#include <stdio.h>
int main ( )
{
int a = 12;
printf ("Value of a=%d\n", a);
printf ("Address of a=%x\n", &a);
return 0;
}

Output
Value of a =12
Address of a = 28ff1c (This output is different for different runs because the variable is stored at a different memory location in different runs.)



Whenever we declared a variable in the C programming language then memory is allocated to that variable. Each memory location has its own address, as we saw in the above program. Assume the variable 'i' is stored at memory location 5698 with the value '12'.

int i = 12;

Pointers in C











A pointer variable holds the address of another variable of the same data type and the value of the pointer variable gets stored in the memory location.



Declaration and Initialization of Pointers in C

Just like variables, pointers are also declared before using in the program. Pointer's name can be anything but they should obey the rules of C.

Syntax to declare pointer variables
data type  *pointer_name;

For example
int *w;
float *x;
char *y;

In int *w, w is an integer data type pointer variable. Similarly, x and y are floating and character data type pointer variable. With the help of asterisk (*) operator, we can access the value of the pointer. Because no value is assigned to these variables, so it contains a garbage value and therefore they point to a memory location as shown in the figure, 

int *z

Pointers in C









Initialization of pointer variables

There are two ways to initialize pointer variables. You can get the memory location of the variable with the help of the reference operator (&) or you can directly assign one pointer variable to other pointer variables.

int   q             // Variable declaration

Pointers in C








Variable 'q' is stored at memory location 2000 which consists a garbage value.

int   *p           // Pointer variable declaration


Pointers in C









Pointer variable 'p' is stored at memory location 3000 with garbage value.

p = &q;           // Initialization of pointer variable



Pointers in C









After initialization, the pointer variable 'p' holds the address of the variable q which is 2000.

int *p = q//Initialize pointer using another pointer

How to access a variable through the pointer variable?

Once an address of a variable is assigned to a pointer, the question arises that how to access the value of the variable using the pointer? This is done by using a unary operator (*) asterisk. This is also known as indirection operator or dereferencing operator.

#include <stdio.h>
int main ( )
{
    // Declaring variable and pointer variable
    int a, *q;

    // Initialize variable a
    a = 12;

    // Initialize pointer variable q
    q = &a;

    printf ("%d\n", *q);          //This will print the value of a
    printf ("%d\n", *&a);     // This will print the value of a
    printf ("%d\n", &a);     // This will print the address of a
    printf ("%d\n", p);       // This will print the address of a
    printf ("%d\n", &p);   // This will print the address of p


    return 0;

}

Size of Pointers in C

In the above section, we discussed what is a pointer in C?  We will now discuss the size of the pointer and why the size of the pointer changes from one compiler to another compiler. The manner in which the size of the integer varies from one compiler to another compiler in C programming, the size of the pointer changes from one compiler to another compiler.


Size of pointers in C






Above figure shows that size of integer and size of the pointer varies from compiler to compiler. If the compiler is 16-bit, it occupies 2 bytes, if it's a 32-bit compiler, it occupies 4 bytes integer and pointer both are same. The reason why the pointer's size depends on the size of the compiler. 16-bit compiler contains 2^16 memory location.

Total location in the 16-bit compiler would be 2^16 = 65,536.

int a = 134;

The value of the integer 134 will be allocated in memory location between 0 to 65,536.

Size of pointers in C











int *ptr;


size of pointers in C











Because the pointer ptr is also the type of integer, the memory location will be allocated between 0 to 65,536.

ptr =&a;

Now, the pointer variable 'ptr' holds the address of the integer variable 'a'.

Size of pointers in C








C program to calculate the size of pointers.

#include <stdio.h>
#include <conio.h>
int main ( ) 
{
int a = 32;
char ch = 'd';
int *b;
char *cp;
b = &a;
cp = &ch;
printf ("size of pointer b is %d and cp is %d\n", sizeof(b), sizeof(cp));
return 0;
}

You all might be thinking that its output will be 1 byte for character pointer 'cp' and 4 bytes for the integer pointer 'b'. But this is not true, both the character pointer and the integer pointer will have the size of 4 bytes. Why do so, let's talk about it.

Size of pointers in C









Whenever you declare a normal variable, size varies from one data type to another type because the type of data you are storing is different.
But, when we are declaring character pointer, integer pointer and float pointer holding the address of variable 'ch', 'a' and 'd'.

char *c = &ch;


i.e. character pointer 'c' holds the address (2034) of 'ch'.

int *= &a;

and integer pointer 'b' holds the address (5896) of the variable 'a'.

double *w = &d;

Pointer variable 'w' holds the address (9325) of the variable 'd'.

Using pointer variables always we are storing the same type of data. All pointer variables hold the address of a normal variable, it is not holding the data, so, what it is holding? It is holding the address 2034, 5896 and 9325. Pointer variable always holds the address, so, it always occupies 2 bytes memory. Pointer size is always fixed because it is holding address instead of data.







































No comments:

Post a Comment