Monday, 21 January 2019

Pointers in C

INTRODUCTION OF POINTERS IN C



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 '&'.


Below we have written a program in which we see 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


Whenever we declared a variable in the C programming language then memory is allocated to that variable. This memory location has its own address, which we just saw above. Assume that system allocated memory location 5698 for a variable 'i'.

int i = 12;


Pointers in C












The computer has to assign 5698 memory location to store the value '12'. 5698 is not only a number, because some other time the computer may choose a different location for storing the value '12'. The variable which holds the address is known as pointer variables. 



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



Declaration and Initialization of Pointers in C


Declaration of pointer variables

Syntax:        data type  *pointer_name;

For example

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

In int *w, w is an integer data type pointer variable. Similarly, the statement float *x and char *y. Due to the declaration, the compiler allocates memory to the pointer variables w, x and y. Because no value is assigned to these locations, so it contains a garbage value and therefore they point to unknown locations as shown:


int *z; 


Pointers in C













Initialization of pointer variables


When the address of a variable is assigned to a pointer variable then this process is known as initialization. All uninitialized pointers contain some garbage values that will be interpreted as memory addresses. These addresses are not valid or point to some wrong values and the compiler is unable to detect these errors, the program with an uninitialized pointer will give wrong results. So, it is important to initialize variables in the program very carefully.



int   q             // Variable declaration


Pointers in C










Memory address 2000 is allocated to variable q which consist of a garbage value.


int   *p           // Pointer variable declaration


Pointers in C









p = &q;           // Initialization of pointer variable



Pointers in C












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

We can also combine the initialization with the declaration. 
int *p = q;


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 address of the integer variable 'a' was sent to pointer 'ptr'.

Size of pointers in C








Similarly, the whole process is for 32-bit and 64-bit compiler.


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 an ordinary 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' storing address 2034.

int *b  = &a;

and integer pointer 'b' storing address 5896.

double *w = &d;

and it holds the address 9325.

Using pointer variables always we are storing the same type of data. All pointer variables hold the address of an ordinary 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.





































































































Recommended Posts

No comments:

Post a Comment