1

I have this struct in C and I made an array from it

//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++;
    }
}


then I want to edit and delete one of it like so

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 ",index);
        scanf("%s",  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,"",0,""};
    if(index>0 && index<=size)
    {
            emp_ptr[index-1]=temp;
    }
    else
    {
        printf("Invalid Index \n");
    }

}

everything works fine except the delete function. It just replaces the deleted employee member with empty values like above :\

I have tried this code that deletes an employee by shifting other items in the array to its position like so.

void DeleteEmployee(struct Employee * emp_ptr, int size)
{

    int index;
    printf("Enter Employee Index: ");
    scanf("%i",&index);
    struct Employee temp={0,"",0,""};
    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");
    }

}

this gives me an error in the compilation

error: assignment to expression with array type

that's because the name and phone member are arrays of char :| and I can't do assignment so tried this

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++;
        }

and it doesn't work fine!!!

Should I make a loop for the name and one phone or there is another way to do this without loops?

7
  • you should use strcpy to assign string to another string Commented May 27, 2021 at 17:46
  • better way to do the deletion is to make a function that copy sourc struct to dest struct, and use it for shifting Commented May 27, 2021 at 17:48
  • If you have three items in an array, the index of an item should be 0, 1, or 2. You should generally not be subtracting 1 before doing array indexing. You're setting yourself up for the dreaded "off by one" error Commented May 27, 2021 at 17:50
  • @TimRandall I assume that index will be entered as a position like 2 --> second item in the array, So I supose to subtract 1. Commented May 27, 2021 at 17:57
  • @JatinParmar so you mean taking the for loop into a function that takes size, array, index !! what should I suppose to change in code in it? please give me more details. Commented May 27, 2021 at 18:01

2 Answers 2

2

So, in C you can't "delete" something from the middle of an array like you can in some other (typically higher level) languages. So yes, your original delete function is really just writing "zeros" over everything so its still there, but otherwise empty. Your second delete function of moving things up in order to delete is frequently what the higher level languages are doing to "update" an array with less entries, and is reasonable. Remember to keep track of how full your array is. IE, when you delete something you'll want to move the upper stuff down, and then keep track that there are now only '2' entries left in the array.

Important: there are two concepts here:

  1. the size of the array that you originally allocated. You don't want to write beyond this entry in the array (or program go boom).
  2. The number of entries you have in it at any time. Typically you'll want to store this value in a second variable.

I think your last example looks like it should work, but because you have an index that starts at 1, you probably want to change the array indexes to add a -1 everywhere.

As for copying data, when you have strings you might look at using strncpy instead of strcpy as it's safer and will prevent bad data from crashing your program when it's bigger than the storage space (for example when Name is bigger than 20).

Finally, C is basically allocated a block of memory for you to store your data in. For an array of structures, it's allocating 3 times the size of your structure. So you can actually do something like this:

memcpy(&emparr[0], &emparr[1], sizeof(struct Employee));

to copy the contents of the n=1 entry in the array to the n=0 spot. It's a faster way to move all the memory.

But: like indicated in #2 above, you must keep track of what entries are "in use" in the structure. You can't actually delete it (without reallocating an array to a different size -- which I won't get into here).

Sign up to request clarification or add additional context in comments.

Comments

0

thanks, @Wes Hardaker

I think your last example looks like it should work, but because you have an index that starts at 1, you probably want to change the array indexes to add a -1 everywhere.

that saved me

so the delete function will be (without reallocating an array to a different size :) )

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");
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.