The problem is that when you write this:
nameslist[0]="John";
You're not actually using the memory you have allocated. "John" is itself a pointer and you're overwriting the adress returned by malloc with this new pointer.
Use the strncpy()
function to copy the string into the memory you allocated.