Wednesday 21 April 2021

C Programming Pointer Tutorial

Pointer in C Programming

Pointers: 

 

Pointer in C

Pointer in c is strong datatype which hold the address of value . Suppose We have an example of normal

variable declaration var1 and var2 (array declaration) . And now we are printing address of both these

variable. 

#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;

}

 Output :

Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6

 What are Pointers?

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is –

<datatype> *<variablename>

Eg.

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

 //Pointer can be also declare with different style for example:

    1. int* p

    2. int *p     //this is popular declaration 

    3. int  * p 

-----------------------------------------------------------------------------------------------------------------

example.

         int *p;

         int a = 10;

         p = a;

          [p will store the address of a ]

How to Use Pointers?

Live Demo

#include <stdio.h>

 int main () {

 int  var = 20;   /* actual variable declaration */

 int  *p;        /* pointer variable declaration */

 p = &var;  /* store address of var in pointer variable*/

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

 /* address stored in pointer variable */

 printf("Address stored in p variable: %x\n", p );

 /* access the value using the pointer */

 printf("Value of *p variable: %d\n", *p );

 return 0;

}

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
 
Example 2:

#include <stdio.h>

#include <conio.h>

 void main () {

 int  i = 100;   /* actual variable declaration */

 int  *p;        /* pointer variable declaration */

 p = &i;  /* store address of var in pointer variable*/

 printf("%d",i); // 100

 printf(“%d”,p); //2046

 printf(“%d”,&i); //2046

 printf(“%d”,&p); //3002

 printf(“%d”,*p); //100

 printf(“%d”,*(&i)); //100

 getch();

}

 

NULL Pointers

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.

#include <stdio.h>
#include <conio.h>
int main () {
int  *ptr = NULL;
printf("The value of ptr is : %x\n", ptr  );
return 0;
}
The value of ptr is 0
 How to check Null Pointer Condition:

To check for a null pointer, you can use an 'if' statement as follows −

if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

 


Previous Post
Next Post
Related Posts

No comments:

Post a Comment