This exercise is based upon dynamic memory allocation
ABC Pvt Ltd. manages employee records of other companies.
Employee Id can be of any length and it can contain any character
for 3 employees, you have to take 'length of employee if' as input as input in a length integer variable.
Then, you have to take employee id as an input and display it one screen.
store the employee id in a character array which is allocated dynamically.
You have to create only one character array dynamically.
Example:
employee 1:
Enter no of characters in you eId
45
dynamically allocate the character array.
thake input from user
do same for other 2 employee
#include <stdio.h>
#include <stdlib.h>
int main()
{
int chars, i = 0;
char *ptr;
while (i < 3)
{
printf("Employee %d: Enter the no. of characters in your Employee ID:",i+1);
scanf("%d",&chars);
getchar(); //often the scanf skips to take the value from user so we use getchar() so that we can consume value.
ptr = (char*) malloc((chars+1)*sizeof(char)); // so as string have null character at end so we need to add extra space for that
printf("Enter you employee ID :");
scanf("%s",ptr); // no & as this is a pointer which is used to store address.
printf("Your Employee ID is %s",ptr);
free(ptr);
i++;
}
return 0;
}