
Newbie getting runtime Exception (STATUS_ACCESS_VIOLATION) trying to implement Ex. 2-4 from K&R2
I'm new to the language, so I'm probably doing something lame,
and it probably involves the pointers I only vaguely realise I'm
implicitely using, but I can't for the life of me determine
what I'm doing wrong. This is a fairly simple piece of code,
an attempt to implement Exercise 2-4 from p 48 of K&R, 2nd ed.
A significant part of this code comes direct from the example
on page 47.
I'd be very appreciative if some guru could whap me on the
head with exactly what I'm doing wrong. Here's the error
I'm getting at runtime:
D:\cygwin\usr\local\src\hellow>squeeze2
0 [main] SQUEEZE2 107087 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
493 [main] SQUEEZE2 107087 stackdump: Dumping stack trace to
SQUEEZE2.EXE.stackdump
I can provide the stackdump, but I figure the error will
be obvious from my actual code (which follows below).
Oh, my compiler version:
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-5/specs
gcc version 2.95.2-5 19991024 (cygwin experimental)
--
TIA,
Jonadab, a Perl, Inform, and elisp programmer trying to learn C.
Source code follows:
/*-----------------------------------------------------------------*/
/* My attempt at K&R2 Exercise 2-4 p 48. */
int charinstring (char c, char t[])
{
int i;
for (i=0; t[i] != '\0'; i++)
if (t[i] == c) return 1;
return 0;
Quote:
}
/* squeeze: delete all chars from s that are in x */
void squeeze(char s[], char x[])
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (! charinstring(s[i], x))
s[j++] = s[i];
s[j] = '\0';
Quote:
}
void testsqueeze (char sentence[], char toss[])
{
/* test squeeze function */
printf ("Squeezing \"%s\" from \"%s\" ", toss, sentence);
squeeze (sentence, toss);
printf ("results in \"%s\".\n", sentence);
Quote:
}
main ()
{
testsqueeze("The quick brown fox jumped over lazy dogs.",
"rstlne");
Quote:
}
--