06 Pointers-All Programs' Listing

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Pointer1-c.

cpp

#include <stdio.h>
#include <conio.h>
int main()
{
// clrscr();
int x = 50;
int * xptr = &x;

printf("Value of x=%d\n",x);
printf("Address of x=%u\n", &x);
printf("Address of x = %u\n", xptr);
printf("Value of x = %d\n", *xptr);

int ** yptr = &xptr;


printf("Value of x = %d\n",**yptr);
printf("Address of xptr = %u \n", &xptr);
printf("Address of xptr = %u \n", yptr);
printf("Address of yptr = %u \n", &yptr);
getch();
};
Pointer2-c.cpp

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

int main()
{
int a;
int * c;
int ** d;
// clrscr();
a = 10;
c = &a;
d = &c;

printf("Value of A = %d\n" , a );
printf("Value of A = %d\n" , *c );
printf("Value of A = %d\n" , **d );
printf("Value of A = %d\n" , *(&a) );

printf("Address of A = %u\n" , &a );


printf("Address of A = %u\n" , c );
printf("Address of A = %u\n" , *d );

printf("Value of C = %u\n" , c );
printf("Address of C = %u\n" , c );

printf("Value of D = %u\n" , d );
printf("Address of D = %u\n" , &d );

// changing value of A with the help of C.


*c = 20;
printf("Value of A = %u\n" , a );

// changing value of A with the help of D.


**d = 30;
printf("Value of A = %u\n" , a );

getch();
};
Pointer3-c.cpp

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

int main()
{
int a,b,c;
void add(int,int,int *);
// clrscr();
a = 10;
b = 20;
add(a,b,&c);
printf("%d + %d = %d\n", a, b, c);
add(50,60,&c);
printf("50 + 60 = %d\n",c);
getch();
}

void add(int x,int y,int * z)


{
*z = x + y;
}
Pointer4-c.cpp

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

int main()
{
int a,b;
void swapv(int,int);
void swapr(int *,int *);

// clrscr();
a = 10;
b = 20;
printf ("Original Values : A = %d\t\t B = %d\n",a,b);
swapv(a,b);
printf ("After swapv : A = %d\t\t B = %d\n",a,b);
swapr(&a,&b);
printf ("After swapr : A = %d\t\t B = %d\n",a,b);
getch();
}

void swapv(int x,int y)


{
int z;
printf ("swapv : X = %d Y = %d\n",x,y);
z = x;
x = y;
y = z;
printf ("swapv : X = %d Y = %d\n",x,y);
}

void swapr(int *x,int *y)


{
int z;
printf ("swapr : X = %d Y = %d\n",*x,*y);
z = *x;
*x = *y;
*y = z;
printf ("swapr : X = %d Y = %d\n",*x,*y);
}

You might also like