【发布时间】:2019-04-19 04:02:21
【问题描述】:
我正在测试下面的代码,但是当执行时,这一行中出现了一个 AV:
*port = 0;
如何解决这个问题?我做错了什么?
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char *host = "127.0.0.1:1234";
char *port = strchr(host, ':');
if (port)
{
*port = 0;
++port;
printf("%s \n", *port);
int portInt = strtol(port, NULL, 10);
printf("%d: \n", portInt);
}
getchar();
return 0;
}
【问题讨论】:
-
您也不需要所有代码来复制错误。修改字符串文字是未定义的行为。即
int main() { char *p = "abc"; p[0] = 'x'; }——同样的问题。