Tutorial references

c
c++
c#

Tutorial references

chapters

Pointer

Pointer

Pointer is a variable which used to store address of another variable.

Two operators are used in pointer:

   (1.) & : It is pronounced as "Address of".

   (2.) * : It is pronounced as "Value at address". The "value at address" operator is also called "indirection" operator.

%u: It is used to print "address".

Pointer variable declaration syntax:

data-type *variableName;

Example:

int *p ;


In below program p variable holds 100.

&m returns the "Address of m".

%p prints the address of m.

*(&m) returns same value as m.

Example

#include <stdio.h>
#include <conio.h>

void main(){
  int m = 100 ;
 clrscr();
 printf("\nAddress of m = %p",&m);
 printf("\nValue of m = %d",m);
 printf("\nValue of m = %d",*(&m));
 getch();
}

Run program

In below program ptr is pointer type variable.

"ptr = &v;" means ptr contains "address of v". Now ptr using in program means we are using address of v. And *ptr using in program means we are using value of v.

Example

#include <stdio.h>
#include <conio.h>

void main(){
  int *ptr ;
  int v = 100 ;
 clrscr();
 ptr = &v ;
 printf ("\nAddress of v = %p",&v) ;
 printf ("\nAddress of v = %p",ptr) ;
 printf ("\nAddress of ptr = %p",&ptr) ;
 printf ("\nValue of ptr = %p",ptr) ;
 printf ("\nValue of v = %d",v) ;
 printf ("\nValue of v = %d",*(&v)) ;
 printf ("\nValue of v = %d",*ptr) ;
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>

void main(){
 int a,b,c;
 int m = 100;
 int *p1,*p2;
 clrscr();
 printf("\n enter value of a: ");
 scanf("%d",&a);
 printf("\n enter value of b: ");
 scanf("%d",&b);
 p1 = &a;
 p2 = &b;
 c = *p1 + *p2 + m;
 printf("\nvalue of c= %d",c);
 getch();
}

Run program

Double pointer( pointer to pointer )

Double pointer contains address of pointer type variable.

Syntax:

data-type **variableName;

In below example **ptr2 is double pointer variable.

Example

int m = 100;

int *ptr1;

int **ptr2;

In below program ptr1 contains address of v and ptr2 contains address of ptr2.

Example

#include <stdio.h>
#include <conio.h>

void main(){
  int *ptr1 ;
  int **ptr2 ;
  int v = 100 ;
 clrscr();
 ptr1 = &v ;
 ptr2 = &ptr1 ;
 printf("\n address of v = %p",ptr1);
 printf("\n address of ptr1 = %p",ptr2);
 getch();
}

Run program

From below program, If we are using *ptr1 in program means we are using value of v and we are using **ptr2 means we are using value of v.

Example

#include <stdio.h>
#include <conio.h>

void main(){
  int *ptr1 ;
  int **ptr2 ;
  int v = 100 ;
 clrscr();
 ptr1 = &v ;
 ptr2 = &ptr1 ;
 printf("\n value of *ptr1 = %d",*ptr1);
 printf("\n value of **ptr2 = %d",**ptr2);
 printf("\n value of v = %d",v);
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>

void main(){
 int a,b;
 int *p1, **p2, *pp1, **pp2;
 int s1,s2,s3;
 clrscr();
  printf("\nenter value of a: ");
 scanf("%d",&a);
 printf("\nenter value of b: ");
 scanf("%d",&b);
  p1 = &a ;
 p2 = &p1 ;
  pp1 = &b ;
  pp2 = &pp1 ;
  s1 = **p2 + **pp2;
 s2 = *p1 + **pp2;
 s3 = s1 + s2;
 printf("\n value of s1 = %d",s1);
 printf("\n value of s2 = %d",s2);
 printf("\n value of s3 = %d",s3);
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018