I have a homework(actually i had 4 months ago but i try to solve this again) that will do these things like in the code, but i must also have to make a function in main that finds the struct of the minimum ages. How can i do this, when after the printf in init function the memory that is blocked is "now"empty and all the names and ages are gone...can anyone help?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct Employee{
char name[30];
int age; // [20-65]
int x;
};
void init(struct Employee *A, int n){
int i;
srand(time(NULL));
for(i=0; i<n; i++){
A[i].age = 20 + rand()%45;
A[i].x = i+1;
printf("Name:\n");
gets(A[i].name);
}
for(i=0; i<n; i++){
printf("%d] Mr/Mrs %s is %d years old,\n", A[i].x, A[i].name, A[i].age);
}
}
int main(int argc, char **argv){
struct Employee *A;
int size;
size = atoi(argv[1]);
printf("The size given is: %d\n", size);
A = (struct Employee *)malloc(size*sizeof(struct Employee));
if(A == NULL){
printf("Error in allocating memory.\n");
exit(1);
}
init(A, size);
free(A);
return 0;
}