I'm trying to create an array of structs that will expand as the user wants to input more. Here is the struct:
struct employee{
char name[20];
int id;
char job[35];
float basePay;
float overTimePay;
float totalPay;
float totalPayBenefits;
int year;
char agency[15];
int count;
};
I have implemented the expanding struct array here (this will continue adding structs to the array for as long as the user specifies to do so):
struct employee *createEmployees(){
//Declare and initialize necessary variables
int i = 1;
char continueChar = 'y';
struct employee *employeeList = malloc(sizeof(employeeList));
//While user selects continue
while(continueChar == 'y'){
char temp;
//reallocate based on how many times the user has continued
if(i != 1){
employeeList = realloc(employeeList, i * sizeof(employeeList));
}
printf("Enter Employee %d name: ", i);
//Clear char from input
scanf("%c",&temp);
//Set employee name
scanf("%[^\n]", employeeList[i-1].name);
//Get user continue selection
printf("Would you like to enter another employee? (y/n): ");
scanf(" %c", &continueChar);
i ;
}
//Keep a count of all employees in list in the first employee cell
employeeList[0].count = i - 1;
//Return formed employees struct array
return employeeList;
}
When I test that all the names are set in main it appears that they are. The count also is correct in main. My problem comes when I pass this array of structs to a separate function to read a file and input the contents into the struct array. Here is main:
int main(){
//Declare an array to hold all employees
struct employee *employeeList = createEmployees();
//Get userID from txt file
getID(employeeList);
}
And here is getID (This function will search the entire employeeInformation.txt file until it finds a match for the given name. It will repeat this X times where X is the count stored in employeeList[0].count):
void getID(struct employee *employeeList){
char line[100];
int stop = 1;
FILE *fptr = fopen("employeeInformation.txt", "r");
if(!fptr){
perror("Error");
exit(0);
}
//skip first line
fgets(line, 100, fptr);
for(int i = 0; i < employeeList[0].count; i ){
while (!feof(fptr) && stop) {
int id;
char name[20];
char job[35];
fscanf(fptr, "%d\t%[^\t]%[^\n]", &id, &name, &job);
printf("%d\t%s\t%s\n", id, name, job);
if(strcmp(employeeList[i].name, name) == 0){
employeeList[i].id = id;
strcpy(employeeList[i].job, job);
stop = 0;
}
fgets(line, 100, fptr);
}
fseek(fptr, 0, SEEK_SET);
stop = 1;
}
fflush(fptr);
fclose(fptr);
}
So my problem is that employeeList[0].count doesn't seem to be passing correctly. I can get the count correctly in main and before I open a file within the getID method, but after I open a file it says the value is equal to some very long number. I think the problem is in one of two places; either I'm allocating the struct array incorrectly or I'm passing the struct array incorrectly. I've tried numerous methods to try to fix this but I just can't seem to wrap my head around what the issue is. Any help with this would be greatly appreciated!
CodePudding user response:
What's the sizeof(employeeList)?
struct employee *employeeList = malloc(sizeof(employeeList));
Try this:
struct employee *employeeList = malloc(sizeof(*employeeList));
CodePudding user response:
- Memory allocation isn't right.
/* create employee list with 4 elements */
size_t members_count = 4;
struct employee *employeeList = malloc(sizeof(struct employee) * members_count);
- Always(!!!) check what malloc returns
/* create employee list with 4 elements */
size_t members_count = 4;
struct employee *employeeList = malloc(sizeof(struct employee) * members_count);
if (NULL == employeeList)
return;
- If you want to increase dynamically number of members better choose other data structure for that. Not array. Take a look at lists. Because array should be placed in continuous block and this not good for memory in some cases.