简体   繁体   中英

Function to remove a struct from an array doesn't work if I pass the array as a parameter

I have a weird problem and I don't know the reason, so I can't think of a solution to fix it.

My problem:

I have a removeEntry function with a array of structs as parameter, but somehow this function doesn't work.

In an earlier version of my code, I declared my array of structs outside the main function (outside every function), and it worked then. But since I now create my array of struct in my main function, I have to give it as parameter, and now my removeEntry function doesn't work.

Code that isn't working:

void removeEntry(struct entry entries[])
{
    int entryNumber;
    int nrBytes = sizeof(entries);
    int arrayLength = nrBytes / sizeof(entries[0]);
    printf("\nEnter entry number to delete: ");
    scanf("%d",&entryNumber);
    while (scanfOnlyNumber(entryNumber) == false || entryNumber == 0 ||  entries[entryNumber].entry_number == 0)
    {
        printf("\nEnter a legit entry number to delete: ");
        scanf("%d", &entryNumber);
        // Tell the user that the entry was invalid
    }
    int i = 0;
    for(i = entryNumber; i < arrayLength - 1; i++)
    {
        entries[i] = entries[i + 1]; //removes element and moves every element after that one place back.
    }
    updateEntryNumber(entries);
    printf("\nEntry %d removed succesfully, and entry numbers updated!\n", entryNumber);
}

My teacher told me that my arraylength calculation doesn't work when I create my array of structs inside my main function (what I do now), but I can't tell why it doesn't work. If anybody can explain that, then I might be able to fix my removeEnty problem by myself.

If anyone wants the working code (where I don't give my array as parameter, because I create my array outside every function), then tell me and I will post it.

Problem

When you pass an array to a function, you don't pass the entire array, instead you pass a pointer to the array. Hence, when you do int nrBytes = sizeof(entries); you're actually getting the size of pointer variable rather than the size of array.

Solution

Pass your array length along with a pointer to the array to the function, something like this:

void removeEntry(struct entry entries[], int arrayLength){
    // your code
}
int nrBytes = sizeof(entries);     // basically size of struct node * 
int arrayLength = nrBytes / sizeof(entries[0]);  

In this sizeof(entries) will give size of struct pointer , and not the array , and also in second statement , it wont correctly .

This is because array decays into pointer after passing it to function and referring it .

What you need to do is calculate number of elements in struct array in main and then pass it to the function .

void removeEntry(struct entry entries[],int arrayLength); 

Calculate arrayLength in main as you do.

if you compile your example, you will probably see a warning like this one:

warning: sizeof on array function parameter will return size of 'entry *' instead of 'entry []' [-Wsizeof-array-argument]

As noted in the answer by V. Kravchenko, this suggests that nrBytes will contain merely the size of the pointer, while sizeof(entries[0]) returns the "true" size of the entire structure. Therefore, arrayLength will be most probably zero.

You should pass array's size to function, because int nrBytes = sizeof(entries); is always 4 or 8 (on x64 systems). It's just pointer's size.

int nrBytes = sizeof(entries);

Arrays of any type (even arrays of structs) decay into pointers when passed to a function. The lenght of this pointer is always fixed and gives no indication as to the lenght whatsoever. So pass the lenght with the function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM