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.
sizeof(entries)is the size of an address that represents the array argumentstruct 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).