Remove characters from string in standard C

I am on a (ubuntu exact) Linux system and I want to remove leading characters (tabs) from a string in C. I thought the following code worked on my previous installation (ubuntu oneric), but I found that it no longer works (note that this is a simplified version of the code for common UTF-8 characters):

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

int main()
{

    int nbytes = 10000;
    char *my_line, *my_char;

    my_line = (char *)malloc((nbytes + 1)*sizeof(char));

    strcpy(my_line,"\tinterface(quiet=true):");
    printf("MY_LINE_ORIG=%s\n",my_line);

    while((my_char=strchr(my_line,9))!=NULL){
        strcpy(my_char, my_char+1);
    }
    printf("MY_LINE=%s\n",my_line);

    return 0;
}

I do

gcc -o removetab removetab.c

When I execute removeetab, I get

MY_LINE_ORIG=   interface(quiet=true):
MY_LINE=interfae(quiet==true):

Note the publication "=" and the missing "c"! What is wrong or how can I achieve this as an alternative. The code must support UTF-8 strings.

+5
source share
2 answers
strcpy(my_char, my_char+1);
Lines

strcpy should not overlap.

From Standard C (Impact Impact):

(C99, 7.21.2.3p2) " strcpy , s2 ( ), , s1. , undefined."

+8

man strcpy:

DESCRIPTION
The  strcpy()  function  copies the string pointed to by src, including
the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.
The  strings  may  not overlap, and the destination string dest must be
large enough to receive the copy.

strcpy() , .

+3

All Articles