char *port = strchr(host, ':');
создает указатель на ':' в строке хоста, и поскольку вы определили буквенную строку,
char *host = "127.0.0.1:1234";`
host
является указателем на чтениетолько ячейка памяти и, таким образом,
*port = 0;
фактически пытается записать в строку только для чтения host
.
вы можете написать:
int _tmain(int argc, _TCHAR* argv[])
{
// define a const as const
const char *host_default = "127.0.0.1:1234";
// dup host if you want to write in it, or change.
char *host=strdup(host_default);
if (!host) exit(-1); // check if memory was allocated!
// port pointer to `:` in memory of host String
char *port = strchr(host, ':');
if (port)
{
*port = 0; // have host to be Null terminated
++port;
printf("%s \n", port);
long portInt = strtol(port, NULL, 10);
printf("Port: %ld: \n", portInt);
// I can only assume you also want the hostname, seen the *port = 0;
printf("HostName: %s: \n", host);
}
// free allocated memory;
free(host);
// set to NULL, good practise
host=NULL;
// set port also to NULL as it might point to released memory of host
port=NULL;
getchar();
return 0;
}