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;

}

No comments:

Post a Comment