Re: Why segfault and no NULL match in for loop?



somebody wrote:

There are two files below named search.c and search.h. In the for
loop in search.c, the for loop never exits, even if
mystruct[i].field1 has no match. Instead of exiting the for loop
it keeps going until it segfaults. This seems to be related to the
strcmp with the NULL value. There are 2 comments below that
indicate the segfaults. I guess the question is, when there is no
match, how to I detect that and return without a segfault?

.... snip to content of search.h ...

struct _mystruct mystruct[] =
{
"AAA", "EEE", "R", "DF", 25,
"BBB", "FFF", "R", "DF", 25,
"CCC", "GGG", "R", "DF", 99,
"DDD", "HHH", "R", "DF", 13,
};

NEVER define an actual object in a .h file. Doing so will cause it
to be multiply defined, and result in link or run time errors. Add
the word "extrn" to the definition, and delete the initialization.
Put the above structure in ONLY one of the files in which you
#include "search.h".

A .h file is NOT a place for headers, it is a place to define the
data you want to export from a .c file.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: Why segfault and no NULL match in for loop?
    ... In the for loop in search.c, the for loop never exits, ... exiting the for loop it keeps going until it segfaults. ... for an empty string or something. ... char field1; ...
    (comp.lang.c)
  • Re: Trying to use STRING
    ... that you do think 'what if', but as Richard says, it CANNOT happen. ... nothing to stop the group level field (ws-balanceX) being initialized ... "A loop-with-exit loop is a loop in which the exit condition ... one for initialization and the other for the inspect itself. ...
    (comp.lang.cobol)
  • Re: LOOP blows!
    ... and initialization of all variables takes place in the prologue. ... the initial `binding' of loop vars for all other FOR-AS ... So FOR-AS-EQUALS differs from other for clauses in another way: ... | tests and the loop body. ...
    (comp.lang.lisp)
  • Re: LOOP blows!
    ... At beginning of each iteration. ... All variables in the FOR clauses ... ] All variables are initialized in the loop prologue. ... Initialization is not assignment! ...
    (comp.lang.lisp)
  • Re: Order of execution question
    ... pass through the loop, not on the entry to the loop. ... The local declaration and initialization of temp. ... I'm not going to tell you to stop making mistakes; ...
    (comp.lang.c)