Re: [Dev-C++] How to get the number of elements in an array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2004-01-20 21:35:38
|
Daniel Glenfield wrote: > And I can't use a for loop and increment the pointer until it reaches > the end of the array because that is not safe, according to Code > Complete. Any more ideas? Maybe this isn't so easy after all! lol As Per Westermark noted, correctly, the ONLY one safe method is to store the size separately. Another (not so safe) alternative might be to use a special array element, which marks the end of the array. This is something, what char[] arrays use: the '\0' character, but you can use whatever you want. BUT, this method is not really the best, as getting the size requires you to iterate through the array, which can be very inefficient at large array sizes. So, just store the size in a separate counter, that's how most people do it, there's no wrong about it, actually, that's the best known method nowadays. You can safely use it. -- Greetings, Balázs |