简体   繁体   中英

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.

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.

When it comes to compiling, it compiles just fine, but gives me segmentation fault: 11 .What's going on? 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:

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:

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) . 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.

In fact, however, the body of the loop dereferences y (indirectly, via printf() ) and increments it. 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. A segmentation fault is a common observed outcome of this particular kind of 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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