我在动态变化的结构中移动指针时遇到了麻烦。我已经创建了我的代码,你可以malloc更多的内存,这似乎是工作。我遇到的问题是如何添加到结构中,如何释放内存以及如何从结构移动到结构并打印所有项目。
我正试图测试添加和打印(删除函数似乎不起作用,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: %dn", i+1);
printf("The employee's name is %s %sn", data->first, data->last);
printf("The employee was hired on: %sn", data->start_date);
printf("The employee make $%fnn", 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("%dn", (sizeof(employee)* rec));
}
第一个问题:Ok…您没有包括雇员类型的声明。我猜first和last被声明为char指针,这是一个错误。你需要它们是固定大小的字符数组,否则这将永远不会工作。为字符串选择一个最大长度,并像这样声明结构。
typedef struct
{
char name[50];
char surname[50];
....
} employee;
以前的文章:嗯,我必须承认我不太喜欢这种实现方式。我会使用更稳定的方法。
首先,由于它只是一个项目列表,您可以使用双重链表。这将允许您添加和删除复杂度为0(1)的元素。真的很好。
这也允许你从第一个到最后迭代所有的项。
为了做到这一点,我将使用更面向对象的方法:)我知道,我们是在C语言中,但听听这个想法。
typedef struct
{
MyList* OwnerList;
Employee* Previous;
Employee* Next;
char name[50];
int age;
} Employee;
typedef struct
{
Employee* First;
Employee* Last;
int Count;
} MyList;
MyList* AllocList() { return calloc(sizeof(MyList), 1); }
void DeleteList(MyList* list)
{
Employee* current;
Employee* next;
for (current = list->First; current != NULL; current = next)
{
next = current->Next;
free(current);
}
free(list);
}
int GetCount(const MyList* list)
{
return list->Count;
}
Employee* AddAmployee(MyList* list)
{
Employee* result = calloc(sizeof(Employee), 1);
Employee* last = list->Last;
if (last != null)
last->Next = result;
else
list->First = result;
result->Previous = last;
list->Last = result;
++list->Count;
result->OwnerList = list;
return result;
}
void RemoveEmployee(Employee* employee)
{
/* i leave removal for you as exercise :) look for doubly linked list */
free(employee);
}
现在,迭代所有项很简单:
Employee* current;
for (current = list->First; current != null; current = current->Next)
printf("%s %dn", current->name, current->age);
您没有使用malloc来分配employee的first, last和start_date属性。因此,当您对deleteData中的指针调用free时,您正在破坏内存。我还会考虑使用链表或其他一些数据结构(如数组)来保存员工记录,从而使您拥有更干净的界面。