C字符串指针函数strdel
有人能告诉我为什么会出现“分段错误…”以及如何在这段代码中修复它吗C字符串指针函数strdel,c,arrays,function,pointers,cstring,C,Arrays,Function,Pointers,Cstring,有人能告诉我为什么会出现“分段错误…”以及如何在这段代码中修复它吗 #include<stdio.h> int str_length(char *s) { int length = 0, i; for(i = 0; *s; i++) { s++; } return i; } char *strdel(char *s, int pos, int n) { int i; char *p, str[] = "";
#include<stdio.h>
int str_length(char *s) {
int length = 0, i;
for(i = 0; *s; i++) {
s++;
}
return i;
}
char *strdel(char *s, int pos, int n) {
int i;
char *p, str[] = "";
p = str;
for(i = 0; i < str_length(s) - n + 1; i++) {
if(i >= pos) {
*(p + i) = *(s + i + n);
}
else {
*(p + i) = *(s + i);
}
}
s = str;
return s;
}
int main() {
char *str = "abcdef";
printf("str_lengh: %d\n", str_length(str));
printf("strdel: %s\n", strdel(str, 1, 2));
return 0;
}
另外,是否有更好的方法来创建函数:
char*strdel(char*s,int-pos,int-n);
从pos位置删除的n个字符比我删除的要多?我想你在这里写的都是
char *strdel(char *s, int pos, int n) {
int i;
char *p, str[] = "";
p = str; // p points to str which is "" and is on the stack with length 0.
for(i = 0; i < str_length(s) - n + 1; i++) {
if(i >= pos) {
*(p + i) = *(s + i + n); // now you are writing onto the stack past p
}
else {
*(p + i) = *(s + i);// now you are writing onto the stack past p
}
}
s = str; // now s points to space on stack
return s; // now you return a pointer to the stack which is about to disapear
}
我想你写得太多了
char *strdel(char *s, int pos, int n) {
int i;
char *p, str[] = "";
p = str; // p points to str which is "" and is on the stack with length 0.
for(i = 0; i < str_length(s) - n + 1; i++) {
if(i >= pos) {
*(p + i) = *(s + i + n); // now you are writing onto the stack past p
}
else {
*(p + i) = *(s + i);// now you are writing onto the stack past p
}
}
s = str; // now s points to space on stack
return s; // now you return a pointer to the stack which is about to disapear
}
为了解决问题的第二部分,我会这样写(假设您要传入字符串常量,因此必须复制传入字符串):
为了解决问题的第二部分,我会这样写(假设您要传入字符串常量,因此必须复制传入字符串):
第二部分我也会给出我的解决方案。这是我的
strdel
char * strdel(char * s, int pos, int n){
memmove(s + pos, s + pos + n, strlen(s) - pos - n + 1);
return s;
}
它不复制,不进行边界检查,返回值相当冗余(因为它等于输入s
)。总而言之,这是非常标准的C库
警告!不能用于字符串常量,因为它修改了
s
(因此没有const char*s
)。我还将为第二部分提供解决方案。这是我的strdel
char * strdel(char * s, int pos, int n){
memmove(s + pos, s + pos + n, strlen(s) - pos - n + 1);
return s;
}
它不复制,不进行边界检查,返回值相当冗余(因为它等于输入s
)。总而言之,这是非常标准的C库
警告!不能用于字符串常量,因为它修改
s
(因此没有const char*s
)。为了解决第二个问题,我会使用memmove()
。你有一个循环,每次迭代都会计算字符串的长度,这是没有效率的。char*strdel(char*s,int-pos,int-n){memmove(s+pos,s+pos+n,strlen(s)-n+1);返回s;}
应该这样做,尽管它不复制。它也不会进行任何边界检查。为了解决第二个问题,我会使用memmove()
。你有一个循环,每次迭代都会计算字符串的长度,这是没有效率的。char*strdel(char*s,int-pos,int-n){memmove(s+pos,s+pos+n,strlen(s)-n+1);返回s;}
应该这样做,尽管它不复制。它也不做任何边界检查。@Mike在问题注释中给出了一个非常有用的提示。:)是的,这也是我一直在考虑的解决方案,直到我看到他在传递字符串常量一个简单的charstr[]=“…”在这种情况下,code>应该解决这个问题。循环直到你点击'\0'
实际上可能会更有效,因为你不必计算字符串的长度,然后再次遍历它。也许,请记住memmove
可能会进行大量优化[需要引用],所以它可能同样快。@Mike在问题评论中给出了一个非常有用的提示。:)是的,这也是我一直在考虑的解决方案,直到我看到他在传递字符串常量一个简单的charstr[]=“…”在这种情况下,code>应该可以解决这个问题。循环直到你点击“\0”
实际上可能更有效,因为你不必计算字符串的长度,然后再次遍历它。也许,请记住memmove
可能经过了大量优化[需要引用],所以它可能同样快。
char * strdel(char * s, int pos, int n){
memmove(s + pos, s + pos + n, strlen(s) - pos - n + 1);
return s;
}