1

I'm trying to "delete" an element from my struct and move the rest of my arrays below it up so it doesn't show a gap.

I can't, unfortunately, paste the code that I am using here. My teacher will be checking for plagiarism, so I really don't want to bother explaining that this is my own written code and risk my grade. I'll just try to explain the code instead.

I've got a code to call a function so I can find the position of the bank account that I wish to delete, which includes ID, first & last name, and balance.
This function also returns an integer, which I have named it c.

In my for loop (j = c; j < totalAccs; j++) I have included all of my arrays and int variables(from my struct), and by using strcpy I replace the element c ( the one that I found from calling another function). i.e. strcpy(a[j].id,a[j+1].id).

I'm pretty sure that it's my loop that is completely wrong, but I am not sure what else to type in there.

My current problem is that whenever I execute this loop it replaces those arrays that I want to replace but it also types out a 0 at the end of my list.

Keilara
  • 25
  • 6
  • 2
    `strcpy` can not be used for `struct`'s and you don't need a loop, [memmove](http://stackoverflow.com/questions/27684119/c-method-for-moving-structs-between-dynamic-arrays) is your friend – David Ranieri Oct 13 '15 at 17:36
  • After removing the element and filling the gap, `totalAccs` will be one less than before. You also shouldn't copy data fom one beyond the original `totalAccs` into the array. – M Oehm Oct 13 '15 at 17:37
  • You don't even need functions: a struct can be equated to another of the same type. – Weather Vane Oct 13 '15 at 17:37
  • My teacher never mentioned memmove, so there's got to be another way. – Keilara Oct 13 '15 at 17:38
  • There is, see previous comment. `mystruct[1] = mystruct[2]`, say. – Weather Vane Oct 13 '15 at 17:38
  • @Keilara So you can use `memmove` because your teacher didn't mentioned it ? – ameyCU Oct 13 '15 at 17:39
  • I'd rather learn the very basics before going into something else so I know what I am doing rather than not knowing. – Keilara Oct 13 '15 at 17:41
  • 1
    As @AlterMann mentioned, you shouldn't be using `strcpy`, mainly because `strcpy` stops copying once it sees a 0 byte. Since you seem to have ints in your struct, your code may very well break if a 0 is present (ie. balance=0). If you are bent on using some sort of memory copy, use `memcpy` instead, it behaves the same as `strcpy` but does not stop at the 0 byte – stack smasher Oct 13 '15 at 17:52
  • For loop copy, back to front, the end of the buffer to the newly created "gap" - which is exactly what memmove does. – Michael Dorgan Oct 13 '15 at 18:03

0 Answers0