无法理解导致此分段错误的原因(C+;+;中的C样式字符串) 这个问题来自教科书:编程——Bjarne Stroustrup用C++(第二版)的原理和实践。< /P>
注意:我已经解决了这个问题,这个问题是因为我不了解我以前尝试解决的方法是如何失败的。我知道这是一个很长的问题,但我觉得理解这个概念很重要 问题是: 编写一个函数char*findx(const char*s,const char*x),该函数在s中查找第一个出现的C样式字符串x 我可以很容易地找到解决方案(或者改变函数的声明方式),但是如果我觉得这个问题是整个练习的重点,那么我就暂缓了。请随意更正我在这个问题上的任何错误陈述。另外,如果这是一个过度复杂的问题,请原谅我,但我真的不明白这一点 对于初学者,以下是我的函数,它是以编译的方式编写的,但在运行时会导致segfault:无法理解导致此分段错误的原因(C+;+;中的C样式字符串) 这个问题来自教科书:编程——Bjarne Stroustrup用C++(第二版)的原理和实践。< /P>,c++,string,pointers,C++,String,Pointers,注意:我已经解决了这个问题,这个问题是因为我不了解我以前尝试解决的方法是如何失败的。我知道这是一个很长的问题,但我觉得理解这个概念很重要 问题是: 编写一个函数char*findx(const char*s,const char*x),该函数在s中查找第一个出现的C样式字符串x 我可以很容易地找到解决方案(或者改变函数的声明方式),但是如果我觉得这个问题是整个练习的重点,那么我就暂缓了。请随意更正我在这个问题上的任何错误陈述。另外,如果这是一个过度复杂的问题,请原谅我,但我真的不明白这一点 对于
char* findx(const char* s, const char* x) {
if (s==nullptr || x == nullptr) return nullptr; //if no string, return nullptr
int s_size = strlen(s);
int x_size = strlen(x);
char* ptr_to_first_occ = nullptr; //initialize pointer to 1st occ
for (int i = 0; i < (s_size-x_size); ++i) { //end with the last char that
//can hold string x to the end
for (int j = i; j < x_size; ++j) { //check if the first char match
if (s[j] != x[j]) ptr_to_first_occ = nullptr; //if it doesn't, set to 0
//and move on
else ptr_to_first_occ = (char*)(s) + i; //set equal to first occ if ==
}
}
if (ptr_to_first_occ) return ptr_to_first_occ;
else return nullptr;
}
char*findx(常量char*s,常量char*x){
如果(s==nullptr | | x==nullptr)返回nullptr;//如果没有字符串,则返回nullptr
int s_size=strlen(s);
int x_size=strlen(x);
char*ptr\u to_first\u occ=nullptr;//初始化指向第一个occ的指针
对于(int i=0;i<(s_size-x_size);++i){//以
//可以将字符串x保持到底
对于(int j=i;j
主要内容如下:
const char* y = "Hey how are you?";
const char* t = "you";
char* p = nullptr;
p = findx(y, t);
cout << *p << endl;
const char*y=“你好吗?”;
const char*t=“你”;
char*p=nullptr;
p=findx(y,t);
C+C++和C是两种不同的语言。请选择一个。您正在学习-因此下一步要学习的是调试器。此示例非常适合单步执行并查看变量如何更改。与其猜测发生了什么,不如使用编译器附带的调试器。第二,我不建议重写代码,也不知道如何解决问题。那只是把东西扔到墙上,希望有东西粘在墙上。找出原始程序失败的原因,然后根据这些信息,重写修复发现错误的代码。此外,C++中还有一个不确定的行为,因为你的代码现在工作并不意味着它真的有效。这可以追溯到找出原始程序失败的原因。需要添加对两个字符串大小的检查。想想当“s”比“x”短时会发生什么(提示:i<(s\u size-x\u size)
)。保罗-我同意,我说过我解决了问题,但没有彻底测试它!我必须说,我没有做随机的事情来看看什么是有效的,我只是忽略了我是如何得到解决方案的一大部分(主要是因为当我开始接近解决方案时,我碰巧非常专注)。
for (int i = 0; i < (s_size-x_size); ++i) {
for (int j = i; j < x_size; ++j) {
if (s[j] != x[j]) ptr_to_first_occ = nullptr;
else {
const char* ptr_to_first_occ = &s[i];
}
}
}
for (int j = i; j < x_size; ++j) {...}
for (int j = i; j < i + x_size; ++j) {...}
char* findx(const char* s, const char* x) {
if (s==nullptr || x == nullptr) return nullptr;
int s_size = strlen(s);
int x_size = strlen(x);
char* ptr_to_first_occ = nullptr;
for (int i = 0; i < (s_size-x_size); ++i) {
for (int j = i; j < i+x_size; ++j) {
if (s[j] != x[j]) ptr_to_first_occ = nullptr;
else {
ptr_to_first_occ = (char*)&s[i];
}
}
}
if (ptr_to_first_occ) return ptr_to_first_occ;
else return nullptr;
}
const char* y = "Hey how are you?";
const char* t = "you";
char* p = nullptr;
p = findx(y, t);
if (p) cout << *p << endl;
char* findx(const char* s, const char* x) {
if (s==nullptr || x == nullptr) return nullptr;
int s_size = strlen(s);
int x_size = strlen(x);
char* ptr_to_first_occ = nullptr;
for (int i = 0; i < (s_size-x_size); ++i) {
if (s[i] != x[0]) ptr_to_first_occ = nullptr;
else {
for (int k = 0; k < x_size; ++k) {
if (s[i+k] == x[k]) ptr_to_first_occ = (char*)&s[i];
else {
ptr_to_first_occ = nullptr;
break;
}
}
}
}
if (ptr_to_first_occ) return ptr_to_first_occ;
else {
return nullptr;
}
}