- Define a cart structure by the name mycart. Also, define two pointers to structure of the cart structure, ptrcart and ptrcust, as shown in the following code snippet:
struct cart mycart;
struct cart *ptrcart, *ptrcust;
- Enter the order number, email address, and password of the customer, and these values will be accepted using the mycart structure variable. As mentioned previously, the dot operator (.) will be used for accessing the structure members, orderno, emailaddress, and password, through a structure variable as follows:
printf("Enter order number: ");
scanf("%d",&mycart.orderno);
printf("Enter email address: ");
scanf("%s",mycart.emailaddress);
printf("Enter password: ");
scanf("%s",mycart.password);
- Set the pointer to the ptrcart structure to point at the mycart structure using the ptrcart=&mycart statement. Consequently, the pointer to the ptrcart structure will be able to access the members of the mycart structure by using the arrow (->) operator. By using ptrcart->orderno, ptrcart->emailaddress, and ptrcart->password, the values assigned to the orderno, emailaddress, and password structure members are accessed and displayed:
printf("\nDetails of the customer are as follows:\n");
printf("Order number : %d\n", ptrcart->orderno);
printf("Email address : %s\n", ptrcart->emailaddress);
printf("Password : %s\n", ptrcart->password);
- We will also modify the email address and password of the customer by asking them to enter a new email address and password and accept the new details via the pointer to the ptrcart structure as follows. Because ptrcart is pointing to the mycart structure, the new email address and password will overwrite the existing values that were assigned to the structure members of mycart:
printf("\nEnter new email address: ");
scanf("%s",ptrcart->emailaddress);
printf("Enter new password: ");
scanf("%s",ptrcart->password);
/*The new modified values of orderno, emailaddress and password members are displayed using structure variable, mycart using dot operator (.).*/
printf("\nModified customer's information is:\n");
printf("Order number: %d\n", mycart.orderno);
printf("Email address: %s\n", mycart.emailaddress);
printf("Password: %s\n", mycart.password);
- Then, define a pointer to the *ptrcust structure. Using the following malloc function, allocate memory for it. The sizeof function will find out the number of bytes consumed by each of the structure members and return the total number of bytes consumed by the structure as a whole:
ptrcust=(struct cart *)malloc(sizeof(struct cart));
- Enter the order number, email address, and password of the customer, and all the values will be assigned to the respective structure members using a pointer to a structure as follows. Obviously, the arrow operator (->) will be used for accessing the structure members through a pointer to a structure:
printf("Enter order number: ");
scanf("%d",&ptrcust->orderno);
printf("Enter email address: ");
scanf("%s",ptrcust->emailaddress);
printf("Enter password: ");
scanf("%s",ptrcust->password);
- The values entered by the user are then displayed through the pointer to the ptrcust structure again as follows:
printf("\nDetails of the second customer are as follows:\n");
printf("Order number : %d\n", ptrcust->orderno);
printf("Email address : %s\n", ptrcust->emailaddress);
printf("Password : %s\n", ptrcust->password);
The following pointertostruct.c program explains how to access a structure by using a pointer:
#include <stdio.h>
#include <stdlib.h>
struct cart
{
int orderno;
char emailaddress[30];
char password[30];
};
void main()
{
struct cart mycart;
struct cart *ptrcart, *ptrcust;
ptrcart = &mycart;
printf("Enter order number: ");
scanf("%d",&mycart.orderno);
printf("Enter email address: ");
scanf("%s",mycart.emailaddress);
printf("Enter password: ");
scanf("%s",mycart.password);
printf("\nDetails of the customer are as follows:\n");
printf("Order number : %d\n", ptrcart->orderno);
printf("Email address : %s\n", ptrcart->emailaddress);
printf("Password : %s\n", ptrcart->password);
printf("\nEnter new email address: ");
scanf("%s",ptrcart->emailaddress);
printf("Enter new password: ");
scanf("%s",ptrcart->password);
printf("\nModified customer's information is:\n");
printf("Order number: %d\n", mycart.orderno);
printf("Email address: %s\n", mycart.emailaddress);
printf("Password: %s\n", mycart.password);
ptrcust=(struct cart *)malloc(sizeof(struct cart));
printf("\nEnter information of another customer:\n");
printf("Enter order number: ");
scanf("%d",&ptrcust->orderno);
printf("Enter email address: ");
scanf("%s",ptrcust->emailaddress);
printf("Enter password: ");
scanf("%s",ptrcust->password);
printf("\nDetails of the second customer are as follows:\n");
printf("Order number : %d\n", ptrcust->orderno);
printf("Email address : %s\n", ptrcust->emailaddress);
printf("Password : %s\n", ptrcust->password);
}
Now, let's go behind the scenes.