Please write a C program that defines and declares a pointer

Please write a C program that defines and declares a pointer to a structure containing three integer members.

Then prompts the user to enter three integers that the program will store in the structure using the pointer variable.

Also, displays the integers on the screen using the pointer variable.

Thank you for the help!

Solution

#include <stdio.h>
#include <stdlib.h>

// structure having 3 integer as num1 num2 num3
struct testStruct {
int num1;
int num2;
int num3;
};

int main()
{
struct testStruct *ptr;// pointer to a structure
  
ptr = (struct testStruct*) malloc(sizeof(struct testStruct));// assigning memory
  
printf(\"Enter num1:\ \");// getting num1
scanf(\"%d\", &(ptr)->num1);
  
printf(\"Enter num2:\ \");// getting num2
scanf(\"%d\", &(ptr)->num2);
  
  
printf(\"Enter num3:\ \");// getting num3
scanf(\"%d\", &(ptr)->num3);
  
  
printf(\"num1: %d\\t num2: %d\\t num3: %d\ \", (ptr)->num1, (ptr)->num2, (ptr)->num3);// printing all the 3 integers using pointer

return 0;
}

-----output-----
Enter num1:   
1   
Enter num2:   
23
Enter num3:   
45
num1: 1 num2: 23 num3: 45
-------output ends-----

Note: Please go through the comments and feel free to ask any doubts. God bless you!!

Please write a C program that defines and declares a pointer to a structure containing three integer members. Then prompts the user to enter three integers that

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site