
char[] vs char * vs #defines for constant strings
Quote:
>In our system, we use tags (or constant strings) at various levels :
> a process tag (usually extracted from argv[0])
> a module tag (one per module, where a module is usually a .c / .h combo)
> a function tag (one per function where required)
>To date, these have been of the form
Do you mean "today" ?
Quote:
>(module example)
>static const char ModuleName[] = "PointDbase"; (at top of .c file)
>(function example)
>static const char FuncName[] = "SetPointsOffScan";
Sounds correct to me.
static const char *const ModuleName[] = "PointDbase";
static const char *const FuncName[] = "SetPointsOffScan";
or
#define MODULE "PointDbase"
#define FUNCTION "SetPointsOffScan"
/* function */
#define FUNCTION
would be correct too...
Quote:
>And these tags are used for logging errors to a general Error handling
>module, viz
> LogMessage( ModuleName, FuncName, BAD_POINT_DETECTED );
>(BAD_POINT_DETECTED would be an enum, used by the Error handler to index
>into a
>string table.)
I was doing that "before", but now (inspired by assert()), I use a macro
that calls the error log function.
With the combination of __FILE__ and __LINE__, I have enough information to
track the bug.
Quote:
>I noticed in various CLC posts that other folk would implement tags as
>static const char *FuncName = "SetPointsOffScan"; (rather than array of
>chars)
Quite similar, but by accident, the pointer could be modified by the
programmer without any warning.
Quote:
>I've also seen this taken further (belt, braces, AND elastic band!!!)
>static const char * const FuncName = "SetPointsOffScan";
This is not a "double const", but it makes the pointer not modifiable.
(prevents from FuncName++ or FuncName=NULL for example)
Quote:
>and also
>#define FUNCTION "SetPointsOffScan"
>STATUS_T SetPointsOffScan( void )
>{
> /*some stuff */
> if ( error detection expression )
> {
> LogMessage( MODULE_NAME, FUNCTION, BAD_POINT_DETECTED );
> }
>} /* SetPointsOffScan */
>#undef FUNCTION
Why not...
Quote:
>I personally don't like the #define FUNCTION method, but what do the gurus
I am just a simple programmer. Can I talk anyway ?
Quote:
>out there
>think and why? Whichever char method is used, static const always
>qualifies it,
>and all code is linted to a fairly high level.
>I suspect the argument of char[] Versus char * for constant strings is
>perhaps a Holy War
Yes, I am afraid...
Quote:
>for some practitioners, but all comments gratefully received
>A second question is this : is there a standard way of replacing the
>explicit string
>form for ModuleName (which must be changed if the .c filename changes) and
>replacing
>it with something which uses __FILE__ but DOESN'T include the path?
AFAIK, it is implementation-dependent. (not all systems have paths)
--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC