У меня проблемы с перемещением указателя в динамически меняющейся структуре.Я создал свой код, где вы можете распределять больше памяти, и это, кажется, работает.Проблемы, с которыми я сталкиваюсь, - это как добавить в структуру, как освободить память и как перейти от структуры к структуре и распечатать все элементы.
Я пытаюсь проверить добавление и печать (функция удаления, которая там находится, похоже, не работает, segfaults)
Когда я добавляю в структуру и затем печатаю структуру, я получаю ошибку сегментаиз значений, которые я добавил.Я не знаю, правильно ли я перехожу из первой структуры в следующую.
#include <stdio.h>
#include <stdlib.h>
#include "pointer.h"
/********************************************
Creates more memory for size (strut * rec+1)
*********************************************/
employee *create(int record){
employee *new_employee = malloc(sizeof(employee) * (record+1));
return new_employee;
}
/********************************************
Copies the data from one structure to a new structure with
size "structure" multipled by rec+1
***********************************************/
employee *copy(employee *data, int record){
employee *new_employee = create(record);
int i;
for(i = 0; i<record;i++){
new_employee->first = data->first;
new_employee->last = data->last;
new_employee->start_date = data->start_date;
new_employee->sal = data->sal;
data++;
}
/********************
Needs to free the old struct
*********************/
//deleteData(data, record);
return new_employee;
}
/********************************************
Function prints everything in the struct
*********************************************/
void printStruct(employee *data, int record){
int i;
for(i = 0; i<record; i++){
printf("\nEntry: %d\n", i+1);
printf("The employee's name is %s %s\n", data->first, data->last);
printf("The employee was hired on: %s\n", data->start_date);
printf("The employee make $%f\n\n", data->sal);
data++;
}
}
/******************************************
Function frees the old data base
*******************************************/
void deleteData(employee *data, int record){
int i;
for(i = 0; i<record; i++){
free(data->first);
free(data->last);
free(data->start_date);
data++;
}
free(data);
}
/******************************************
Adds an employee to the new structure
*******************************************/
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record){
employee *employeeDB = create(record);
employeeDB = copy(data, record);
int i;
employeeDB++;
employeeDB->first = fname;
employeeDB->last = lname;
employeeDB->start_date = date;
employeeDB->sal = salary;
return employeeDB;
}
/**************************
Starts of the main function
***************************/
int main(void){
//Keeps track of the number of records that are in the structure
int rec = 0;
//Keeps the number of accesses to the structure. Even with the one entry the structure has not been accessed.
int acc = 0;
//Holds the input information for the menu
int input;
//holds the information for inputing user first name
char *fname;
//holds the information for inputing user last name
char *lname;
//holds the information for for the startdate
char *start;
//holds the information for the salary;
float sal;
/*********************************
This next section adds an employee to the record
************************************/
//This creates the first entry to the dynamic structure.
employee *first_employee = create(rec);
first_employee->first = "FIRST";
first_employee->last = "LAST";
first_employee->start_date = "June-20th-2006";
first_employee->sal = 55555.55;
//increase the number of records
rec = rec+1;
employee *new_employeeDB = add(first_employee, "fname", "lname", "JUNE-20th-2010", 55555.55, rec);
rec = rec + 1;
printStruct(new_employeeDB, rec);
printf("%d\n", (sizeof(employee)* rec));
}