delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/04/11:38:20

From: "Art S. Kagel" <kagel AT bloomberg DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie Question: Index lookup coord. checking...
Date: Fri, 1 Aug 1997 09:55:53 -0400
Lines: 37
Message-ID: <Pine.D-G.3.91.970801095031.16551D-100000@dg1>
References: <5rif6n$5v1 AT news DOT velocity DOT net>
NNTP-Posting-Host: 160.43.8.60
Mime-Version: 1.0
In-Reply-To: <5rif6n$5v1@news.velocity.net>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Mon, 28 Jul 1997, Badman wrote:

> Hello, I have a simple question: Can you check coordinates for an index
> lookup (so you don't get an error) and check that same index in the same
> if() statement?
> 
> For example:
> if (x > 0 && x < 640 && y > 0 && y < 480 && *(screen+(y * 640)+x) > 3){
> 
>    /* do stuff */
> };
> 
> or....
> 
> 
> if (x > 0 && x < 640 && y > 0 && y < 480){ 
>   if (*(screen+(y * 640)+x)>3){
> };
> };
> 
> which is better? Any help would be appreciated...
 
Both are good in the sense that both will work exactly the same way.
However, the latter (separate if's) is MUCH easier to read an
maintain and no less efficient at runtime.   BTW:

	*(screen+(y * 640)+x)
and
	screen[y][x]

are not only equivalent, but the GCC optimizer can actually generate
better runtime code from the array form in some situations.  Try the
timings yourself.  It is rarely slower and also MUCH easier to read
and maintain.

Art S. Kagel, kagel AT bloomberg DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019