如何解决删除在 C
我在 C 中有这个结构体,我用它创建了一个数组
//Structure DeFinition
struct Employee
{
int Id;
char Name[20];
float Salary;
char Mobile[12];
};
int main()
{
int emparr_size = 3;
struct Employee emparr[emparr_size];
AddAllEmployees(emparr,emparr_size);
displayAllEmployees(emparr,emparr_size);
EditEmployee(emparr,emparr_size);
DeleteEmployee(emparr,emparr_size);
}
void AddAllEmployees(struct Employee * emp_ptr,int size)
{
for(int i=0; i<size; i++)
{
printf("Enter Employee %i Id: ",i+1);
scanf("%i",&emp_ptr->Id);
printf("Enter Employee %i Name: ",i+1);
scanf("%s",emp_ptr->Name);
printf("Enter Employee %i Salary: ",i+1);
scanf("%f",&emp_ptr->Salary);
printf("Enter Employee %i Mobile: ",emp_ptr->Mobile);
emp_ptr++;
}
}
void displayAllEmployees(struct Employee * emp_ptr,int size)
{
for(int i=0; i<size; i++)
{
printf("Employee %i Id is %i \n",i+1,emp_ptr->Id);
printf("Employee %i Name is %s \n",emp_ptr->Name);
printf("Employee %i Salary is %f \n",emp_ptr->Salary);
printf("Employee %i Mobile is %s \n",emp_ptr->Mobile);
emp_ptr++;
}
}
void EditEmployee(struct Employee * emp_ptr,int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
if(index>0 && index<=size)
{
printf("new Employee %i Id ",index);
scanf("%i",&emp_ptr[index-1].Id);
printf("new Employee %i Name ",index);
scanf("%s",emp_ptr[index-1].Name);
printf("new Employee %i Salary ",index);
scanf("%f",&emp_ptr[index-1].Salary);
printf("new Employee %i Mobile ",emp_ptr[index-1].Mobile);
}
else
{
printf("Invalid Index \n");
}
}
void DeleteEmployee(struct Employee * emp_ptr,int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
struct Employee temp={0,"",""};
if(index>0 && index<=size)
{
emp_ptr[index-1]=temp;
}
else
{
printf("Invalid Index \n");
}
}
除了删除功能外,一切正常。它只是用上面的空值替换已删除的员工成员:\
我已经尝试过通过将数组中的其他项目移动到其位置来删除员工的代码。
void DeleteEmployee(struct Employee * emp_ptr,""};
if(index>0 && index<=size)
{
for(int i=0; i<size-1; i++) // important i < size-1
{
emp_ptr[index-1].Id=emp_ptr[index+1+i].Id;
emp_ptr[i+index].Name=emp_ptr[index+1+i].Name;
emp_ptr[i+index].Salary=emp_ptr[index+1+i].Salary;
emp_ptr[i+index].Mobile=emp_ptr[index+1+i].Mobile;
emp_ptr++;
}
}
else
{
printf("Invalid Index \n");
}
}
error: assignment to expression with array type
那是因为 name 和 phone 成员是字符数组:|我不能做作业 所以试过这个
for(int i=0; i<size-1; i++) // important i < size-1
{
emp_ptr[i+index].Id=emp_ptr[index+1+i].Id;
strcpy(emp_ptr[index+1+i].Name,emp_ptr[i+index].Name);
emp_ptr[i+index].Id=emp_ptr[index+1+i].Salary;
strcpy(emp_ptr[index+1+i].Mobile,emp_ptr[i+index].Mobile);
emp_ptr++;
}
它不能正常工作!!!
我应该为姓名和一部电话制作一个循环,还是有另一种没有循环的方法?
解决方法
因此,在 C 中,您不能像在其他一些(通常是更高级别的)语言中那样从数组中间“删除”某些内容。所以是的,你原来的删除函数实际上只是在所有东西上写了“零”,所以它仍然存在,但除此之外是空的。您的第二个删除功能是向上移动以删除内容,这是高级语言经常使用较少条目来“更新”数组的操作,并且是合理的。记住要跟踪您的阵列有多满。 IE,当您删除某些内容时,您需要将上面的内容向下移动,然后跟踪数组中现在只剩下“2”个条目。
重要提示:这里有两个概念:
- 您最初分配的数组的大小。您不想在数组中写入超出此条目的内容(或程序 go 热潮)。
- 您在任何时候的条目数。通常,您需要将此值存储在第二个变量中。
我认为您的最后一个示例看起来应该可行,但是因为您有一个从 1 开始的 index
,您可能希望更改数组索引以在任何地方添加一个 -1
。
至于复制数据,当您有字符串时,您可能会考虑使用 strncpy
而不是 strcpy
,因为它更安全,并且可以防止坏数据大于存储空间时使程序崩溃(例如例如,当 Name
大于 20 时)。
最后,C 基本上分配了一块内存供您存储数据。对于结构数组,它分配的大小是结构大小的 3 倍。所以你实际上可以做这样的事情:
memcpy(&emparr[0],&emparr[1],sizeof(struct Employee));
将数组中 n=1 条目的内容复制到 n=0 位置。这是移动所有内存的更快方法。
但是:如上面#2 所示,您必须跟踪结构中“正在使用”的条目。您实际上无法删除它(无需将数组重新分配为不同的大小——我不会在这里讨论)。
,谢谢@Wes Hardaker
我认为你的最后一个例子看起来应该可以工作,但是因为你有一个从 1 开始的索引,你可能想要更改数组索引以在任何地方添加 -1。
救了我
因此删除函数将是(无需将数组重新分配到不同的大小:))
void DeleteEmployee(struct Employee * emp_ptr,int size)
{
int index;
printf("Enter Employee Index: ");
scanf("%i",&index);
if(index>0 && index<=size)
{
for(int i=0; i<size-1; i++) // important i < size-1
{
emp_ptr[i+index-1].Id=emp_ptr[index+i].Id;
//strcpy does'nt prevent bad data from crashing your program when it's bigger than the storage space (for example when Name is bigger than 20).
emp_ptr[i+index-1].Salary=emp_ptr[index+i].Salary;
strncpy(emp_ptr[i+index-1].Name,emp_ptr[index+i].Name,size);
strncpy(emp_ptr[i+index-1].Mobile,emp_ptr[index+i].Mobile,size);
emp_ptr++;
}
struct Employee temp={0,"",""};
emp_ptr[index-1]=temp;
}
else
{
printf("Invalid Index \n");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。