86.5k views
1 vote
Defines a structure using typedef. The structure should have an integer as a member and a 50-character array for holding a string. Ask the user how many of these structures are needed. Using malloc, create storage for those number of structures (remember a structure is like any other variable – int or float). Fill the structures with a number and a string using scanf. In a function, print out the contents of the structures.

1 Answer

4 votes

Answer:

see explaination

Step-by-step explanation:

#include<stdio.h>

#include<stdlib.h>

typedef struct emp // STRUCTURE DEFINITION

{

int id;

char name[50];

}var;

//DISPLAY OF INFORMATION FUNCTION DEFINITION

void display(var *e[],int); // PASSING ARRAY OF POINTERS OF TYPE STRUCTURE AND SIZE OF STRUCTURE

int main()

{

system("clear");

printf("*************** WELCOME TO STRUCTURE PROGRAM *********\\\\");

printf("****** ENTER THE NUMBER OF STRUCTURES REQUIRED\\");

printf("******ENTER THE INFORMATION\\");

printf("******GET THE OUTPUT\\\\");

printf("\\******************************************************\\\\");

int size,i;

printf("ENTER THE NUMBER OF STRUCTURES REQUIRED:");

scanf("%d",&size);

var *temp[size]; // POINTER OF TYPE STRUCTURE DECLARED

for(i=0;i<size;i++)

{

temp[i]=(var *)malloc(sizeof(var)); // MEMORY ALLOCATION AND TYPE CASTING TO STRUCTURE TYPE

printf("ENTER THE ID OF THE STUDENT:");

scanf("%d",&temp[i]->id);

printf("ENTER THE NAME OF THE STUDENT:");

scanf("%s",temp[i]->name);

}

display(temp,size); // CALLING DISPLAY FUNCTION

}

void display(var *e[],int size) // DISPLAY FUNCTION DECLARATION

{

int i;

for(i=0;i<size;i++)

{

printf("\\");

printf("**********************************************\\\\");

printf("ID : %d\t NAME : %s\\\\",e[i]->id,e[i]->name);

printf("**********************************************\\\\");

}

}

User Thiago Krempser
by
4.2k points