Arrays 删除C中具有char数组成员的结构数组中的项

Arrays 删除C中具有char数组成员的结构数组中的项,arrays,c,pointers,struct,char,Arrays,C,Pointers,Struct,Char,我用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

我用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: ", i+1);
        scanf("%s", 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",i+1, emp_ptr->Name);
        printf("Employee %i Salary is %f \n",i+1, emp_ptr->Salary);
        printf("Employee %i Mobile is %s \n",i+1, emp_ptr->Mobile);
        emp_ptr++;
    }
}


这是因为name和phone成员是char:|的数组,我不能执行赋值 所以我试过这个

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++;
        }
for(int i=0;i因此,在C语言中,不能像在其他(通常是更高级)语言中那样从数组中间“删除”某些内容。因此,是的,您最初的删除函数实际上只是在写“零”你的第二个删除功能是为了删除而向上移动东西,这通常是高级语言用来“更新”的一个具有较少条目的数组,并且是合理的。请记住跟踪数组的填充量。例如,当您删除某个内容时,您将希望向下移动上面的内容,然后跟踪数组中现在只剩下“2”条目的情况

要点:这里有两个概念:

  • 您最初分配的数组的大小。您不希望在数组(或程序)中写入超过此项的内容
  • 在任何时候都包含在其中的条目数。通常,您希望将此值存储在第二个变量中
  • 我认为您的上一个示例看起来应该可以工作,但是因为您有一个从1开始的
    索引,所以您可能希望更改数组索引,以便在所有位置添加
    -1

    至于复制数据,当您有字符串时,您可能会考虑使用
    strncpy
    而不是
    strcpy
    ,因为这样更安全,并且当坏数据大于存储空间时(例如
    Name
    大于20),可以防止坏数据崩溃程序

    最后,C基本上分配了一块内存供您存储数据。对于一个结构数组,它分配的内存是结构大小的3倍。因此,您实际上可以这样做:

    memcpy(&emparr[0], &emparr[1], sizeof(struct Employee));
    
    将数组中n=1项的内容复制到n=0点。这是移动所有内存的更快方法

    但是:正如上面第2条所指出的,您必须跟踪结构中“正在使用”的条目。您不能实际删除它(如果不将数组重新分配到不同的大小,我将不在这里进行讨论)。

    谢谢,@Wes Hardaker

    我认为您的最后一个示例看起来应该可以工作,但是因为您有一个从1开始的索引,所以您可能希望更改数组索引,以便在所有位置添加-1

    那救了我

    因此,delete函数将是(无需将数组重新分配为不同的大小:)

    void DeleteEmployee(结构雇员*emp_ptr,整数大小)
    {
    整数指数;
    printf(“输入员工索引:”);
    scanf(“%i”和索引);
    
    如果(index>0&&indexy您应该使用strcpy将字符串分配给另一个字符串执行删除操作的更好方法是创建一个函数,将sourc struct复制到dest struct,并使用它进行移位。如果数组中有三个项,则一个项的索引应为0、1或2。通常在进行数组索引之前不应减去1。您正在设置为可怕的“一个接一个”做好准备error@TimRandall我假设索引将作为数组中2-->第二项的位置输入,所以我想减去1。@JatinParmar那么你的意思是将for循环引入一个采用大小、数组、索引的函数!!我应该在其中的代码中做什么更改?请提供更多详细信息。
    error: assignment to expression with array type
    
    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++;
            }
    
    memcpy(&emparr[0], &emparr[1], sizeof(struct Employee));
    
    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,"",0,""};
            emp_ptr[index-1]=temp;
        }
        else
        {
            printf("Invalid Index \n");
        }
    
    }