Hello, friends:
I want to strip a character from a string, copying in place except the unwanted char with the following code:
// test.c
#include <stdio.h>
#include <windows.h>
void stripChar(char *s, char ch)
{
char * t;
t = s;
if (!ch) return;
while ((*s = *t++))
{
if (*s != ch) s++;
}
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
char *s = "This is a test";
FILE *df = NULL;
df = fopen("test.txt","a");
fputs(s, df);
stripChar(s, 'a');
fputs(s, df);
fclose(df);
return EXIT_SUCCESS;
}
The program runs, but it does not produce the expected result, a file with "This is a testThis is test". The file test.txt is empty
I started the debugger and the program does not enter the while loop. At the end of stripChar I get "Exception: Access violation"
I tried this same program in the tcc compiler and it works.
Any ideas?