So I have this Contact struct and an array that holds a bunch of instances of Contact. My problem is that I use memcpy and have tried using mmove for this as well to "delete" and "add" contact elements to this array. It seems to work perfectly fine when I debug and run through the program. I can track my contacts being added and removed from the array but when I run the program without debug and without stepping through the program crashes!
This is my Contact struct:
typedef struct contact Contact;
typedef struct contact *pContact;
struct contact {
char lastName[BUFSIZ];
char firstName[BUFSIZ];
char email[BUFSIZ];
char pNumber[BUFSIZ];
};
This is how a contact is created:
struct Contact *contactArr[1024];
int size = 0;
Contact* CreateContact(int pos, char *info) {
Contact *pContactNewContact = (Contact*) malloc(sizeof(Contact));
char *lastName = strtok(info, ",");
char *firstName = strtok(NULL, ",");
char *email = strtok(NULL, ",");
char *pNumber = strtok(NULL, ",");
if (pContactNewContact) {
strcpy(pContactNewContact->lastName, lastName);
strcpy(pContactNewContact->firstName, firstName);
strcpy(pContactNewContact->email, email);
strcpy(pContactNewContact->pNumber, pNumber);
}
InsertContact(pos, pContactNewContact);
return pContactNewContact;
}
These are my array manipulating functions.
void InsertContact(int pos, pContact *insert) {
if (size == 0)
contactArr[0] = insert;
else {
memmove((contactArr + pos + 1), (contactArr + pos),
(size + 1) * sizeof(Contact));
contactArr[pos] = insert;
}
size++;
}
void DelContact(int pos) {
if (pos == 0) {
memmove(contactArr, (contactArr + 1), (size - 1) * sizeof(Contact));
contactArr[pos] = 0;
} else if (pos <= size) {
memmove((contactArr + pos - 1), (contactArr + pos),
(size - pos) * sizeof(Contact));
contactArr[pos] = 0;
}
size--;
}