In: Computer Science
I am comfortable with structures that contains one item per variable. But i am struggling to manage structure that contains multiple items in one variable (within a matrix). it's in C language.
for example this structure :
struct resume{
char *name;
char *job;
char school*;
int counter_resume;
}resume;
i want to create functions that are able to dynamically allocate memory so it can add multiple persons resume without knowing how many they are going to be in the beginning. Lets say we have Bob . Bob worked as a fireman, postman, and truck driver. So there is 3 items in the Jobs variable for Bob. let say he went to school to Marveric and Standford. We should be able to add more persons in the same structure using the functions. for example the main look like this :
nb: feel free to give more knowledge, suggestions, or any useful advices or information needed for me to master this particular subject.
int main()
{
struct resume *Bob = malloc(sizeof(struct resume));
add_name(Bob,"Bob");
add_job(Bob, "Postman");
add_job(Bob, "Truck driver");
add_job(Bob, "fireman");
add_school(Bob, "Standford University");
add_school(Bob, "Chicago business school");
print_single_resume(Bob); // print Bob resume
print_all_resume(); // print all of the resumes
free_memory(bob); // function that free the memory allocated
free_all(); // function that free the memory allocated for all
the persons
}
We can use arrays to store more than one values for the same data type. Here we need to keep the count of elements in the array. The initial count is 0 and when we add elements to the array, increment the count by 1. The array index starts with 0. We can add each element at the specified index of the array. We need to specify the maximum size of the array at the time of declaration.
#include<stdio.h>
#include<stdlib.h>
struct resume{
char *name;
char *job[10]; // array of
jobs(maximum 10 jobs
int jobCount; // count
of jobs
char *school[10]; // array of school(maximum 10
schools)
int schoolCount; // count of schools
int counter_resume;
}resume;
// array of resume and the count of resumes
struct resume* resumes[10];
int resumeCount = 0;
// function to add name to a resume
void add_name(struct resume *r, char* name){
r->name = name;
}
// function to add job to a resume
void add_job(struct resume *r, char* job){
r->job[r->jobCount++] = job;
}
// function to add school to a resume
void add_school(struct resume *r, char* school){
r->school[r->schoolCount++] =
school;
}
// function to print a resume
void print_single_resume(struct resume *r){
printf("\nName: %s\n", r->name);
printf("Jobs: ");
int i;
for(i=0; i<r->jobCount-1; i++){
printf("%s,",
r->job[i]);
}
printf("%s\n", r->job[i]);
printf("Schools: ");
for(i=0; i<r->schoolCount-1; i++){
printf("%s,",
r->school[i]);
}
printf("%s\n", r->school[i]);
}
// function to print all resumes
void print_all_resume(){
int i;
for(i=0; i<resumeCount; i++){
print_single_resume(resumes[i]);
}
}
// function to free memory of a resume
void free_memory(struct resume *r){
free(r);
}
// function to free memory of all resumes
void free_all(){
int i;
for(i=0; i<resumeCount; i++){
free_memory(resumes[i]);
}
}
int main()
{
struct resume *Bob = malloc(sizeof(struct
resume));
add_name(Bob,"Bob");
// initialize counts to 0
Bob->jobCount = 0;
Bob->schoolCount = 0;
add_job(Bob, "Postman");
add_job(Bob, "Truck driver");
add_job(Bob, "fireman");
add_school(Bob, "Standford
University");
add_school(Bob, "Chicago business school");
// add Bob to resumes
resumes[resumeCount++] = Bob;
struct resume *Jay = malloc(sizeof(struct
resume));
// initialize counts to 0
Jay->jobCount = 0;
Jay->schoolCount = 0;
add_name(Jay,"Jay");
add_job(Jay, "Postman");
add_school(Jay, "Standford University");
// add Jay to resumes
resumes[resumeCount++] = Jay;
printf("\nBob's resume:");
print_single_resume(Bob); // print Bob
resume
printf("\n\nAll resumes:\n");
print_all_resume(); // print all of the
resumes
free_memory(Bob); // function that free the memory allocated
free_all(); // function that free the memory
allocated for all the persons
}
Output