Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #94287

Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault
Date 2016-09-30 14:22 +0100
Organization A noiseless patient Spider
Message-ID <871t01a5fo.fsf@bsb.me.uk> (permalink)
References <75ada694-4a58-4f1d-81d1-9490a709e75d@googlegroups.com>

Show all headers | View raw


alla.rashitova@gmail.com writes:

> I don't understand why I get segmentation fault on my while loop
> while(source[i] != '\n' || source[i] != '\0')
> when i use it to run through the line, which is stored in an array.

A loop ends when it's condition is false, so *every time you write a loop*
negate the condition and check that what you get is indeed the state of
affairs that you want to bring about.

To negate a condition, you need to know two rules called De Morgan's
laws.  They state that the "not of an or is the and of the nots"

  !(A || B)  ==  !A && !B

and the "not of and and is the or of the nots":

  !(A && B)  ==  !A || !B

so your loop it trying to bring about a state in which

  source[i] != '\n' || source[i] != '\0'

is false, i.e.

  !(source[i] != '\n' || source[i] != '\0')

De Morgran tell us that this means

  !(source[i] != '\n') && !(source[i] != '\0')

and we may as well turn !(X != Y) into X == Y while we are at it:

  source[i] == '\n' && source[i] == '\0'

So your loop is trying to make source[i] be both newline and null at the
same time.  It won't ever be able to do that no matter what it does.
The loop can never end (unless it's got a break, goto or return in it).

The problem comes from human language.  People often say things like "if
it's not too hot or too cold" but this does translate into formal logic
as

  !(too_hot(temperature) || too_cold(temperature))

There is an implied extra "not" in the phrase: "if it's not too hot or
(it is) not too cold" making it

  !too_hot(temperature) || !too_cold(temperature)

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

while(source[i] != '\n' || source[i] != '\0') segmentation fault alla.rashitova@gmail.com - 2016-09-30 05:23 -0700
  Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Alla _ <modelling.data@gmail.com> - 2016-09-30 05:27 -0700
  Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-30 14:22 +0100
    Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault mark.bluemel@gmail.com - 2016-09-30 06:37 -0700
      Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Alla _ <modelling.data@gmail.com> - 2016-09-30 07:49 -0700
      Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Keith Thompson <kst-u@mib.org> - 2016-09-30 09:16 -0700
        Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-30 14:41 -0400
    Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-30 06:59 -0700
    Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Alla _ <modelling.data@gmail.com> - 2016-09-30 07:46 -0700
      Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-30 16:04 +0100
  Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Ken Brody <kenbrody@spamcop.net> - 2016-09-30 10:55 -0400
  Re: while(source[i] != '\n' || source[i] != '\0') segmentation fault Barry Schwarz <schwarzb@dqel.com> - 2016-09-30 09:34 -0700

csiph-web