head = (struct contact *)malloc(sizeof(struct contact));
ptrList = head;
strcpy(head->fName,fn);
strcpy(head->sName,sn);
strcpy(head->phone,ph);
strcpy(head->company,co);
You are derefferencing head
without head
's members have been initialized, thats undefined behavior and probably causing your error because strcpy
is expecting a \0
terminated array. But since you haven't initialized the members of head, you can't know they are terminated or not. And even if they are, the problem si that strcpy()
has to read for checking the \0
and as far it isn't initialized, strcp()
is reading uninitialized memory anyway.
And finaly a standard quote:
J.2 Undefined behavior
1 The behavior is undefined in the following circumstances:
[..]
The value of the object allocated by the malloc function is used (7.20.3.3).
So a solution would be memset()
head to 0
so the memblock will be everywhere terminated, as long you don't override it.