0

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.

1
  • 1
    sizeof(entries) is the size of an address that represents the array argument struct entry entries[]. It doesn't give the actual length of the array (which isn't known by the argument). You'll need to pass the length of the array as a separate argument (or have a way to "find" the last entry). Commented Nov 16, 2015 at 13:48

5 Answers 5

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1
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.

2 Comments

thank you! yeah I was trying some things, that why I edit the 2nd call to "entries[0]"
@user3590152 Yes , but that would be helpful and correct if you pass it from main .
1

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.

Comments

0

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.

Comments

0
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.

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.