Reputation: 2647
I am on a (ubuntu precise) linux system and I want to remove leading characters (tabulators) from a string in C. I thought the following code was working on my previous installation (ubuntu oneric) but I found now that it does not work anymore (note that this is a simplified version of a code for general 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 executing removetab I get
MY_LINE_ORIG= interface(quiet=true):
MY_LINE=interfae(quiet==true):
Note the dublication of "=" and the missing "c"! Whats wrong or how can I achieve this alternatively. The code should support UTF-8 strings.
Upvotes: 5
Views: 860
Reputation: 136515
If you look at 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.
The code invokes strcpy()
on the same array which leads to string corruption.
Upvotes: 3
Reputation: 145899
strcpy(my_char, my_char+1);
strcpy
strings must not overlap.
From the C Standard (emphasis mine):
(C99, 7.21.2.3p2) "The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined."
Upvotes: 8