TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALEX WALKER
from: DARIN MCBRIDE
date: 1997-12-10 18:42:00
subject: Finding the end of an array?

 AW>      Just wondering, if an array is passed to a function, is there a way 
 AW>      to test for or find the end of the array without having the size of
 AW>      the array passed to the function?
 AW>      If it's a char string one could search/test for the null character,
 AW>      but what if it's an array of integers?
In a string, we have decided (I use the term "we" loosely since neither you 
nor I decided this) to use the nul as a terminator.  We have decided that nul 
won't appear anywhere in a useful string, so it can be used as the 
termination character.
If, however, you have binary data - where the nul character could exist - 
there is no way to determine the end of data since all data is "valid".
Your array of integers is the same way.  If it is a "string" of integers (you 
have a terminating value), you can detect that terminator.  Otherwise you 
cannot determine the end.  For example, a string of integers:
#define END_OF_INT_STRING -999
int array[] = {
  0, 15, 39, -35, -2512, 539, 95, -53, END_OF_INT_STRING
};
(Usually in a case like this, we would use INT_MIN or INT_MAX for the end of 
string value, but if we need to use that value, we can create our own 
terminator value that we know is not used anywhere else.)
Good luck,
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.