[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C program segfaults when run, not under GDB
- To: linux-il(at-nospam)linux.org.il
- Subject: Re: C program segfaults when run, not under GDB
- From: Oleg Goldshmidt <ogoldshmidt(at-nospam)computer.org>
- Date: 25 Nov 2001 13:15:56 -0500
- Delivered-To: linux.org.il-linux-il@linux.org.il
- In-Reply-To: <3C00BBB9.4060905@bezeqint.net>
- Organization: Speaking for myself only.
- Original-Sender: ogoldshmidt@computer.org
- References: <3C00BBB9.4060905@bezeqint.net>
- Reply-To: linux-il(at-nospam)linux.org.il
- Sender: ogoldshmidt(at-nospam)computer.org
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
- User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Cuyahoga Valley)
Eugene Romm <sf_milesteg@bezeqint.net> writes:
> I've written a procedure that's supposed to remove all occurances of
> string2 from string1 (parameters).
I'd use sth like strstr(3) for that... Is it homework?
> Segfault occurs on line 29, as far as I can tell.
And that would be
if (factor!=0) s1[c1]=s1[c1+factor];
I guess, where you attempt to write into s1. You passed a string
literal ro squeezechar2(), and that's a const. Think: main() only
knows of a const string "abcdeFghijklmnopqrstuvwxyZ", and passes
a pointer to it (don't let the "char s1[]" form of the argument
confuse you - it is a pointer that is passed, see the comp.lang.c FAQ)
to squeezechar2(). The latter is not allowed to write into the memory
pointed to.
> main() {
> squeezechar2("abcdeFghijklmnopqrstuvwxyZ","deF");
> }
int main(void) { /* yes, int main() and C-style comments... */
char s1[] = "abcdeFghijklmnopqrstuvwxyZ";
return squeezechar2(s1,"deF");
}
> squeezechar2(s1,s2)
> char s1[];
> char s2[];
I thought nobody used traditional C nowadays...
OK, there goes your homework, but this is absolutely, positively the
last time... ;-) Next time, please post to comp.lang.c or something of
the kind (this is not a C list), tell them what you tried, and do read
the FAQ before you post. Thanks,
--
Oleg Goldshmidt | ogoldshmidt@NOSPAM.computer.org
If it aint't broken it hasn't got enough features yet.
=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il