Algorithm
Step 1 : Initialize two integer variables x and y with some values.
Step 2 : Create a pointer iptr of integer type
Step 3 : Assign the address of x to iptr
Step 4 : Print the value and address stored in iptr
Step 5 : Assign the address of y to iptr
Step 6 : Print the value and address stored in iptr
Step 7 : Print the storage requirement of iptr, address of iptr and main function
Program (ptrvar.c)
/* Pointer variables */
# include<stdio.h>
main()
{
int x, y, *iptr;
x = 125;
iptr = &x ; /* iptr points to x */
y = 23;
float *fptr;
printf("X value is %d and stored at %u\n", x, &x);
printf("Y value is %d and stored at %u\n", y, &y);
printf("\nInt pointer holds the address %u\n", iptr);
printf("Aceesing value thru pointer : %d\n", *iptr);
iptr = &y; /* iptr points to y */
printf("\nInt pointer now holds the address %u\n", iptr);
printf("Accessing value thru pointer : %d\n", *iptr);
printf("\nSize of int pointer: %d bytes", sizeof(iptr));
printf("\nSize of float pointer: %d bytes", sizeof(fptr));
printf("\n\nAddress of main function is %u\n", main);
}
Output
[vijai@localhost pointer]$ gcc ptrvar.c
[vijai@localhost pointer]$ ./a.out
X value is 125 and stored at 3221216452
Y value is 23 and stored at 3221216448
Int pointer holds the address 3221216452
Aceesing value thro pointer : 125
Int pointer now holds the address 3221216448
Accessing value thro pointer : 23
Size of int pointer: 4 bytes
Size of float pointer: 4 bytes
Address of main function is 134513448
Step 1 : Initialize two integer variables x and y with some values.
Step 2 : Create a pointer iptr of integer type
Step 3 : Assign the address of x to iptr
Step 4 : Print the value and address stored in iptr
Step 5 : Assign the address of y to iptr
Step 6 : Print the value and address stored in iptr
Step 7 : Print the storage requirement of iptr, address of iptr and main function
Program (ptrvar.c)
/* Pointer variables */
# include<stdio.h>
main()
{
int x, y, *iptr;
x = 125;
iptr = &x ; /* iptr points to x */
y = 23;
float *fptr;
printf("X value is %d and stored at %u\n", x, &x);
printf("Y value is %d and stored at %u\n", y, &y);
printf("\nInt pointer holds the address %u\n", iptr);
printf("Aceesing value thru pointer : %d\n", *iptr);
iptr = &y; /* iptr points to y */
printf("\nInt pointer now holds the address %u\n", iptr);
printf("Accessing value thru pointer : %d\n", *iptr);
printf("\nSize of int pointer: %d bytes", sizeof(iptr));
printf("\nSize of float pointer: %d bytes", sizeof(fptr));
printf("\n\nAddress of main function is %u\n", main);
}
Output
[vijai@localhost pointer]$ gcc ptrvar.c
[vijai@localhost pointer]$ ./a.out
X value is 125 and stored at 3221216452
Y value is 23 and stored at 3221216448
Int pointer holds the address 3221216452
Aceesing value thro pointer : 125
Int pointer now holds the address 3221216448
Accessing value thro pointer : 23
Size of int pointer: 4 bytes
Size of float pointer: 4 bytes
Address of main function is 134513448
No comments:
Post a Comment