Article View: comp.std.c
Article #3446Re: Multi-level break and elimination of GOTO
From: John E. Davis
Date: Fri, 06 Oct 1995 09:53
Date: Fri, 06 Oct 1995 09:53
49 lines
1843 bytes
1843 bytes
On 20 Sep 1995 09:02:37 -0700, Hey Sister can you spare a hug? <awozniak@galaxy.csc.calpoly.edu> wrote:
: int x, y;
: char map[MAXY][MAXX];
:
: /* something filles in my 2D char array and I want to find the first
: asterix inside it. */
:
: for (x = 0; x < MAXX; x++)
: {
: for (y = 0; y < MAXY; y++)
: {
: if (map[y][x] == '*')
: goto found_it;
: }
: }
:
: found_it:
: /* do more stuff */
I have been C writing programs for several years now. My experience indicates
that you should write code that is meant to be read. It is quite obvious
what is intended above. Nevertheless, I probably would not code it that
way--- simply because the assumption that you have made above (that `*'
is present in the array) may become invalid as the code evolves. With this in
mind, I would write:
for (x = 0; x < MAXX; x++)
{
for (y = 0; y < MAXY; y++)
{
if (map[y][x] != '*') continue;
/* found it. Deal with it here--- perhaps by calling
* a function.
*/
.
.
return 0;
}
}
error ("Not found.");
return -1;
Always check for errors. It will save you alot of grief.
Incidently, if speed were an issue, I would use memchr and deal with pointer
arithmetic.
--John
Technical Details
Message-ID:
<452uaf$rqe@senator-bedfellow.MIT.EDU>Path:
arc1-ca.pugleaf.net!space.mit.edu!davisReferences:
<43pdut$hoh@galaxy.csc.calpoly.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!space.mit.edu!davis From: davis@space.mit.edu (John E. Davis) Newsgroups: comp.std.c,comp.lang.c Subject: Re: Multi-level break and elimination of GOTO Date: 6 Oct 1995 09:53:19 GMT Organization: Center for Space Research Message-ID: <452uaf$rqe@senator-bedfellow.MIT.EDU> References: <43pdut$hoh@galaxy.csc.calpoly.edu> Reply-To: davis@space.mit.edu NNTP-Posting-Host: wiwaxia.mit.edu X-Newsreader: slrn (0.7.9.0)