I don't see why I keep getting a segmentation fault when I try to use ANY string function with a character pointer variable. I am fully aware that fgets() is better to ensure no overwriting occurs but I am just trying to write a program to demonstrate how string functions actually overwrite data in memory addresses whereas re-declaring the pointer variable i.e pValues = "New Name"; will point to a completely different set of addresses (that contain your required characters).
I know fully well that string functions do seem to work with pointer constants i.e. when defined like this:
char values[ ] = "Name Here";
BUT WHY do they not work for pointer variables i.e when defined like this:
char *pValues[ ] = "Name Here";
this is the part that keeps failing...
#include <stdio.h>
#include <string.h> // required for strcpy(), strcat(), strlen()
main()
{
char *pName = "Peter Jones";
printf("The string pName is \'%s\'\n\n", pName);
// Now lets change it to a longer string with more litterals...
pName = "Dragons Den is GREAT!";
printf("\nThe string pName is %s\n", pName);
puts("Please enter a new string for pName ");
gets(pName);
printf("pName is now %s\n", pName); // THIS PRODUCES SEGMENTATION FAULT
return 0;
}
The book I am learning from has NO mention of dynamic memory or heap - which I haven't yet covered.
