Write a program in C to demonstrate the use of & (address of) and *(value at address) operator.
#include <stdio.h>
void main()
{
int num=785;
float real = 387.91;
char letter = ‘A’;
int *ptr1;
float *ptr2;
char *ptr3;
ptr1= #
ptr2=ℜ
ptr3=&letter;
printf ( ” num = %d\n”,num);
printf ( ” real = %f\n”,real);
printf ( ” letter = %c\n”,letter);
printf(“\n Using \’&\’ Address of value :\n”);
printf ( ” Address of num = %p\n”,&num);
printf ( ” Address of real = %p\n”,&real);
printf ( ” Address of letter = %p\n”,&letter);
printf(“\n Using \’&\’ and \’*\’ Value at address :\n”);
printf ( ” Value at address of num = %d\n”,*(&num));
printf ( ” Value at address of real = %f\n”,*(&real));
printf ( ” Value at address of letter = %c\n”,*(&letter));
printf(“\n Using only pointer variable :\n”);
printf ( ” Address of num = %p\n”,ptr1);
printf ( ” Address of real = %p\n”,ptr2);
printf ( ” Address of letter = %p\n”,ptr3);
printf(“\n Using only pointer operator :\n”);
printf ( ” Value at address of num = %d\n”,*ptr1);
printf ( ” Value at address of real= %f\n”,*ptr2);
printf ( ” Value at address of letter= %c\n\n”,*ptr3);
}
Output:
num = 785
real =387.91
letter = A
Using ‘&’ Address of value:
Address of num = 0x7ffe2135ca30
Address of real = 0x7ffe2135ca34
Address of letter = 0x7ffe2135ca2f
Using ‘&’ and ‘*’ Value at address:
Value at address of num = 785
Value at address of real = 387.91
Value at address of letter = A
Using only pointer variable:
Address of num = 0x7ffe2135ca30
Address of real = 0x7ffe2135ca34
Address of letter = 0x7ffe2135ca2f
Using only pointer operator:
Value at address of num = 785
Value at address of real= 387.91
Value at address of letter= A