【发布时间】:2016-05-10 12:13:06
【问题描述】:
我有一个字符串,我正在迭代寻找一个特定的单词,它恰好在两个空格之间。
例如:
// where the word that I'm looking for is /docs/index.html
const char* c = "GET /docs/index.html HTTP/1.1\r\n";
我找到这个词如下;
const char* wbeg = strchr(c, ' ') + 1; // points to '/'
const char* wend = strchr(wbeg, ' ') -1; // points to 'l'
如果我想将该单词存储到另一个位置,我使用 strncpy 实现了这一点
char word[256];
strncpy(word, wbeg, wend - wbeg);
我收到以下错误
在 0x00007FFAAE8C4A74 (ucrtbased.dll) 处引发异常 ConsoleApplication1.exe: 0xC0000005: 访问冲突写入位置 0x0000000000000000.
【问题讨论】:
-
您检查
strchr()的返回是否为NULL? -
char word[256];-->char word[256]="";,strncpy(word, wbeg, wend - wbeg);-->strncpy(word, wbeg, wend - wbeg + 1); -
@stanna:如果代码在函数之前中断,为什么不发布相关代码?
-
是时候学习如何使用调试器了。
-
提供的代码没有错误。当然,
wordafterstrncpy()的某些使用会失败并导致“访问冲突写入位置 0x0000000000000000”。投票结束,因为这篇文章缺少“重现所需的最短代码”
标签: c pointers exception strncpy