On Fri, 13 Feb 2004 13:03:42 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Do you prefer *my_string or my_string[0]? Why?
First off, I'm wondering whether you're asking about the limited case
of fetching just the first character of an array, or the more general
question "when is it better to traverse an array with a pointer vs.
using subscripts"? But for the most part, I think both questions share
the same answer [although the latter general question invites
performance-related arguments, most of which probably wouldn't stand
up to close scrutiny in this group ;-) ]
I'd ask myself whether one way or the other better reflects the task
at hand ("better" in this case having a high probability of being
decided on a subjective basis) and/or contributes to a more consistent
approach. Let's see, an example would be good...
#include <ctype.h>
int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}
So first I'd decide whether I wanted to represent the parameter as an
array or as a pointer, and then I'd be consistent in the body of the
function. Or, the process may run in reverse: I'll first find the more
natural notation for the implementation, and then make sure the
declaration is consistent with /that/.
In the example above, I really don't have a preference between
declaring str as an array or as a pointer, but whichever I picked, I'd
just try to be consistent.
Leor Zolman
BD Software
le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at
www.bdsoft.com/tools/stlfilt.html