Program 1: Write a
program to print address of variable using pointer.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int
i=12;
int
*ptr;
clrscr();
printf("\ni
= %d", i);
ptr
= &i;
printf("\nAddress
of i = %u", &i);
printf("\nAddress
of ptr = %u", &ptr);
printf("\nValue
contains by ptr = %d", *ptr);
printf("\nValue
of i = %d", i);
getch();
}
Output:
Program 2: Write a C program to swap the two values using pointers.
Solution:
#include <stdio.h>
#include<conio.h>
void swap(int* a, int* b);
void main( )
{
int
x = 10, y = 5;
clrscr();
printf("before
function call value of x & y:");
printf("\nx
= %d", x);
printf("\ny
= %d", y);
swap(&x,
&y); //Function Call
printf("\nafter
function call value of x & y:");
printf("\nx
= %d", x);
printf("\ny
= %d", y);
getch();
}
void swap(int* a, int* b) //Function Definition
{
int
temp;
temp
= *a;
*a
= *b;
*b
= temp;
}
Output:
Program 3: Write a C program to print the address of character and the character of string using pointer.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
char *ch;
int i=0;
clrscr();
printf("Enter String :
");
gets(str);
ch=str;
while(*ch!='\0')
{
printf("\n
Position : %u Character : %c", ch, *ch);
ch++;
}
getch();
}
Output: