Saturday, 13 July 2019

What is pointers?

A pointer is a variable which denotes the address in the memory of another variable. 
When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. 

#include <stdio.h>

int main ()

{

int var1;

char var2[10];

printf("Address of var1 variable: %x\n", &var1 );

printf("Address of var2 variable: %x\n", &var2 );

return 0;

}

 

include <stdio.h>

in the above code is compiled and executed, it produces the following result:

Address of var1 variable: bff5a400

Address of var2 variable: bff5a3f6




Application of pointer-

Accessing data by their address for example
Parameters are argument of function main. 

Null Pointer-

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program:

 

#include <stdio.h>

int main ()

{

int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;

}

 

When the above code is compiled and executed, it produces the following result:

 

The value of ptr is 0

1 comment:

  1. Thanks Sir,I have learnt pointer deeply by your blog.. thank you again😃😄

    ReplyDelete