In: Computer Science
Please write a complete C coding program (NOT C++) that has the following: (including comments)
- declares two local integers x and y
- defines a global structure containing two pointers (xptr,
yptr) and an integer (z)
- declares a variable (mst) by the type of previous structure
- requests the values of x and y from the user using only one scanf
statement
- sets the first pointer in the struct to point to x
- sets the second pointer in the struct to point to y
- uses the structure to print out the values in x and y (do not
print x and y values directly)
- Declare a variable (mstptr) that is a pointer to the structure and make it point to the structure you already declared (mst)
- Using the pointer to the structure set the integer z in the structure to the number 5.
- Using the pointer to the structure display the value of the integer and the values pointed to by the two pointers (must use -> operator)
#include<stdio.h>
struct Point // declare and define a structure named Point with elements as *xptr, *yptr and z
{
int *xptr;
int *yptr;
int z;
};
int main()
{
int x,y; // declare variables x and y
struct Point mst; // declare structure element mst
printf("Enter value of x and y : ");
scanf("%d%d",&x,&y); // read x and y
values
mst.xptr = &x; // assign the first pointer in the
struct to point to x
mst.yptr = &y; // assign the second pointer in the
struct to point to y
printf("Printing the value using mst
pointer\n"); // display x and y values using the pointer
mst
printf(" x = %d\n",*mst.xptr);
printf(" y = %d\n",*mst.yptr);
struct Point *mstptr=&mst; //Declare a variable mstptr that is
a pointer to the structure
mstptr->z=5; //assign value 5 to
z
printf("\nPrinting the value using structure
pointer\n");
//display the values of x and y and z using structure
point
printf(" x = %d\n",*mstptr->xptr);
printf(" y = %d\n",*mstptr->yptr);
printf(" z = %d\n",mstptr->z);
return 0;
}