【发布时间】:2014-04-06 05:43:38
【问题描述】:
大家好,我正在编写一个程序来读取大学作业的 NMEA 句子,但我遇到了分段错误的问题。谁能帮我解决一下,好吗?
NmeaSentence::NmeaSentence(std::string sentence) {
const char *temp = sentence.c_str();
char *c_sent;
strcpy(c_sent, temp);
char *pch;
for(int i = 0; i < MAX_SENTENCE_PARTS; i++){
pch = strtok(c_sent, ",");
this->sentenceParts[i] = pch;
}
this->sentence = sentence;
this->sentenceType = sentenceParts[0];
}
错误似乎发生在 strcpy。我做错了什么?
【问题讨论】:
-
strcpy with malloc? 的可能重复项
-
附带说明,为什么不在任何地方使用
std::string?是否有特定需求迫使您通过(const) char *操作字符串? -
您使用的是
strtok。那是大错特错了。考虑使用 Boost.Tokenizer,或来自 Boost.StringAlgo 的split。 -
@Will 看来您没有阅读所有答案。
标签: c++