我正在用C编写类似于通讯录的程序,该程序使您可以插入,编辑和删除联系人。它使用动态数组和动态分配。我在重新分配和释放内存时遇到问题。我正在发布主要功能和简化结构。
MAIN
int main() {
int i;
PeopleArray data;
dynamicArrayInit(&data);
while(i != 2){
printf("\n Insert 0 to add, 1 to edit, 2 to finish");
scanf("%d", &i);
switch(i){
case 0: appendP(&data, newP()); break;
case 1: editP(&data, data.item); break; //This doesn't work
}
return 0;
}
结构:1用于存储个人信息; 2个动态数组
typedef struct{
char name[MAX_NAME];
int number;
double height;
}Person;
typedef struct{
Person *item;
int elementsAmount;
}PeopleArray;
初始化数组的函数
void dynamicArrayInit(PeopleArray *array){
array->item = NULL;
array->elementsAmount = 0;
}
分配元素的功能
void appendPerson(PEOPLEARRAY *array, Person p){
array->elementsAmount++;
if(array->item == NULL) {
array->item = (Person *) malloc(array->elementsAmount * sizeof(Person));
}
else {
array->item = (Person *) realloc(array->item, array->elementsAmount * sizeof(Person));
}
if(array->item == NULL) {
exit(-1);
}
array->item[array->elementsAmount-1] = p;
}
创建新元素的功能
Person newPerson(){
Person p;
printf("\n Name:");
scanf("%64[^\n]s",p.name);
checkInput(); //Cleans the buffer, it's a function
printf ("\n Number:");
scanf("%d", p.number);
checkInput();
printf("\n height:");
scanf("%lf", p.height);
checkInput;
return p;
}
要编辑的功能
Person editPerson(PEOPLEARRAY *array, Person *p) {
Person newP;
int i, j, c;
for (i = 0; i < arrayDimension(array); i++) {
printf("\n %d ->%s", i, p->name);
}
printf("\n Insert your choice");
scanf("%d", &j);
checkInput();
for (i = 0; i <= j; i++) {
if (i == j) {
newP.name == p->name;
newP.number == p->number;
newP.height == p->height;
printf("\nWhich field do you want to edit? 0 = name, 1 = number, 2 = height");
scanf("%d", &c);
switch(c){
case 0: printf("\nThe person's name is %s what do you want to change it to?", p->name);
scanf("%64[^\n]s", newP.name); //I assign a new name.
break;
case 1: printf("\nThe person's number is %d what do you want to change it to?", p->number);
scanf("%64[^\n]s", newP.number); //I assign a new number.
break;
case 2: printf("\nThe person's height is %lf what do you want to change it to?", p->height);
scanf("%64[^\n]s", newP.height); //I assign a new height.
break;
}
free (p);
appendPerson(array,newP);
}
return newP;
}
我所做的基本上是:打印所有名称,询问他们要编辑的名称,然后让他们编辑他们选择的名称,但首先我将所有旧值分配给新结构现在,此后,仍然在editPerson
功能中,我应该:A)调配老人;B)分配新的;
但是,如果这样做,在编译并尝试之后,它不会取消分配该结构,甚至不会分配新的结构,而是会再次分配旧的结构。那么,我在做什么错,如何正确编辑此结构?
我自己找到了解决方案。编辑功能
Person editPerson(PEOPLEARRAY *array, Person *p) {
Person newP;
int i, j, c, k;
for (i = 0; i < arrayDimension(array); i++) {
printf("\n %d ->%s", i, p->name);
}
printf("\n Insert your choice");
scanf("%d", &j);
checkInput();
for (i = 0; i <= j; i++) {
if (i == j) {
newP = *p;
for(k=j; k<(array->elementsAmount-1); k++){
array->item[k] = array->item[k+1];
}
if(array->item != NULL) {
array->elementsAmount--;
array->item = (Person *) realloc(array->item, array->elementsAmount * sizeof(Person));
}
printf("\nWhich field do you want to edit? 0 = name, 1 = number, 2 = height");
scanf("%d", &c);
switch(c){
case 0: printf("\nThe person's name is %s what do you want to change it to?",newP.name);
scanf("%64[^\n]s", newP.name); //I assign a new name.
break;
case 1: printf("\nThe person's number is %d what do you want to change it to?", newP.number);
scanf("%64[^\n]s", newP.number); //I assign a new number.
break;
case 2: printf("\nThe person's height is %lf what do you want to change it to?", newP.height);
scanf("%64[^\n]s", newP.height); //I assign a new height.
break;
}
}
return newP;
}
Main
int main() {
int i;
PeopleArray data;
dynamicArrayInit(&data);
while(i != 2){
printf("\n Insert 0 to add, 1 to edit, 2 to finish");
scanf("%d", &i);
switch(i){
case 0: appendP(&data, newP()); break;
case 1: appendP(&data,editPerson(&data, data.item)); break;
}
return 0;
}