简体   繁体   English

从C中的字符串中删除字符

[英]Removing a character from a string in C

So I'm doing a past paper question about functions/characters/pointers. 所以我正在做一个关于函数/字符/指针的纸质问题。 I've come across a problem in my code and need help understanding it. 我在代码中遇到问题,需要帮助来理解它。

More specifically, I am having problems with question B) ii), which requires me removing a colon from a string. 更具体地说,我在问题B)ii)中遇到问题,这要求我从字符串中删除冒号。

My idea was to take the string, make a pointer and point directly to the colon, then replace this colon with the next character that proceeds it. 我的想法是取字符串,做一个指针并直接指向冒号,然后用继续它的下一个字符替换此冒号。 Then replacing the other characters after the colon with the character ahead of it, until it reaches NULL. 然后用冒号前面的字符替换冒号后面的其他字符,直到达到NULL。

When it comes to compiling, it compiles just fine, but gives me segmentation fault: 11 .What's going on? 在编译时,它可以编译,但是给我分段错误:11。这是怎么回事? how can I fix this? 我怎样才能解决这个问题? Is there something I am not understanding? 有我不明白的东西吗? Thanks. 谢谢。

Question

#include <stdio.h>
#include <stdlib.h>


int locate_colon(char* x) //Question B) i)
{
    int i = 0;
    while(*x != '\0')
    {
        if(*x == ':')
        {
            return i;
        }
        i++;
        x++;
    }
    return -1;
}

char* remove_colon(char* x) //Question B) ii)
{
    int y,i,j;
    y = locate_colon(x);
    i =0;
    j =1;
    while(*x != '\0')
    {
        x[y+i] = x[y+j];
        i++;
        j++;
    }
    return x;
}

int main() //Testing if functions work by running through compiler
{
    int x;
    char colon[] = "Colon: 123";
    char* colonptr;
    colonptr = colon;
    x = locate_colon(colonptr);
    printf("%d",x);

    //B) ii)
    char* y;
    y = remove_colon(colonptr);
    while(y != '\0')
    {
        printf("%s",y);
        y++;
    }
    return 0;
}

Your loop in remove_colon() should be: 您在remove_colon()循环应为:

while(x[y+i] != '\0')
{
    x[y+i] = x[y+j];
    i++;
    j++;
}
x[y+i]= '\0';

and

while(y != '\0')
{
    printf("%s",y);
    y++;
}

should just be: 应该只是:

printf("%s",y);

or another option: 或其他选择:

printf("%s",colonptr);

In his now-deleted answer, @Giorgi pointed out this fragment of your code, which is valid C, but is nevertheless quite wrong: @Giorgi在他现在删除的答案中指出了您的代码片段,该片段是有效的C语言,但还是很错误的:

while(y != '\0')
{
    printf("%s",y);
    y++;
}

Variable y is a pointer to char , so while (y != '\\0') is equivalent to while (y != NULL) , which some people would prefer to write as simply while (y) . 变量y是指向char的指针,因此while (y != '\\0')等效于while (y != NULL) ,有些人希望将其写为while (y) C does not define any way in which incrementing a non-null pointer will yield a null pointer, so if the behavior otherwise were well defined then this loop would never exit. C没有定义增加非null指针将产生null指针的任何方式,因此,如果以其他方式定义了良好的行为,则此循环将永远不会退出。

In fact, however, the body of the loop dereferences y (indirectly, via printf() ) and increments it. 但是,实际上,循环的主体取消了y的引用(间接通过printf() )并对其进行递增。 Before too many iterations, y will point outside the memory allocated to the object it originally pointed (in)to, and after that, the behavior of dereferencing it is undefined. 在进行太多迭代之前, y会指向分配给它最初指向的对象的内存之外,此后,解引用该对象的行为是不确定的。 A segmentation fault is a common observed outcome of this particular kind of UB. 分割错误是这种特定类型的UB的常见观察结果。

I suspect that you meant to write while(*y != '\\0') , but that still seems odd, for you would end up printing a sequence of tails of the string, whereas it seems you want to print just one string. 我怀疑您打算写while(*y != '\\0') ,但这仍然很奇怪,因为您最终将打印出字符串的尾部序列,而似乎只想打印一个字符串。 In that case, it is unclear to me why you are using a loop here at all. 在那种情况下,我不清楚您为什么在这里使用循环。 What's wrong with simply 简单有什么问题

printf("%s",y);

?

Of course, that's not the only thing wrong with your code, as has been pointed out in comments, but it likely explains the segmentation fault that is the subject of your actual question. 当然,正如注释中指出的那样,这并不是代码唯一的问题,但它可能解释了作为实际问题主题的分段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM