char[] vs char * vs #defines for constant strings 
Author Message
 char[] vs char * vs #defines for constant strings

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

(module example)
static const char ModuleName[] = "PointDbase"; (at top of .c file)

(function example)
static const char FuncName[] = "SetPointsOffScan";

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 noticed in various CLC posts that other folk would implement tags as

static const char *FuncName = "SetPointsOffScan"; (rather than array of
chars)

I've also seen this taken further (belt, braces, AND elastic band!!!)

static const char * const FuncName = "SetPointsOffScan";

and also

#define FUNCTION "SetPointsOffScan"
STATUS_T SetPointsOffScan( void )
{
   /*some stuff */

   if ( error detection expression )
   {
      LogMessage( MODULE_NAME, FUNCTION, BAD_POINT_DETECTED );
   }

Quote:
} /* SetPointsOffScan */

#undef FUNCTION

<next function, and so on>

I personally don't like the #define FUNCTION method, but what do the gurus
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
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?

All suggestions much appreciated.



Sun, 11 Aug 2002 03:00:00 GMT  
 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



Sun, 11 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
On Wed, 23 Feb 2000 22:43:59 +0100, "-hs-"

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" ?

"To date" is an english expression meaning "up to and including the
present moment in time".

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Sun, 11 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
Mark McIntyre a crit dans le message

Quote:
>>>To date, these have been of the form
>>Do you mean "today" ?
>"To date" is an english expression meaning "up to and including the
>present moment in time".

I see... a sort of "until now" but in better form. Nice to learn something
new every day. Thanks.

--
-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



Mon, 12 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
On Thu, 24 Feb 2000 07:55:31 +0100, "-hs-"

Quote:

>Mark McIntyre a crit dans le message

>>>>To date, these have been of the form
>>>Do you mean "today" ?
>>"To date" is an english expression meaning "up to and including the
>>present moment in time".

>I see... a sort of "until now" but in better form.

Sort of, except that "until now" would usually imply that it had now
stopped happening, while "to date" implies that its still going on.

"To date, I've been male and I still am. Until now I was drunk but now
I'm sober."

Quote:
>Nice to learn something new every day. Thanks.

Mon plaisir.

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 12 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
On Thu, 24 Feb 2000 07:55:31 +0100, "-hs-"

Quote:
>Mark McIntyre a crit dans le message

>>>>To date, these have been of the form
>>>Do you mean "today" ?
>>"To date" is an english expression meaning "up to and including the
>>present moment in time".

>I see... a sort of "until now" but in better form. Nice to learn something
>new every day. Thanks.

Well, sort of.  Here's my take on the two phrases -

"until now" is used when something *was* true, but now is not.  Such
as, "Until now, I had never read newsgroups before."

"To date" is used when something was and is still true.  Usually it's
used when there's an expectation that it will change in the future.
Such as, "To date,  there have been no women presidents."

-Chris



Mon, 12 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
On Fri, 25 Feb 2000 00:18:28 +0100, "-hs-"

Quote:

>Mark McIntyre a crit dans le message

>>"To date, I've been male and I still am. Until now I was drunk but now
>>I'm sober."

>How do you know that ? ;-)

Which part? :-?

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 12 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
Mark McIntyre a crit dans le message

Quote:
>"To date, I've been male and I still am. Until now I was drunk but now
>I'm sober."

How do you know that ? ;-)

--
-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



Tue, 13 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings

: On Thu, 24 Feb 2000 07:55:31 +0100, "-hs-"

:>Mark McIntyre a crit dans le message

:>>>>To date, these have been of the form
:>>>Do you mean "today" ?
:>>"To date" is an english expression meaning "up to and including the
:>>present moment in time".
:>
:>I see... a sort of "until now" but in better form. Nice to learn something
:>new every day. Thanks.

: Well, sort of.  Here's my take on the two phrases -

: "until now" is used when something *was* true, but now is not.  Such
: as, "Until now, I had never read newsgroups before."

: "To date" is used when something was and is still true.  Usually it's
: used when there's an expectation that it will change in the future.
: Such as, "To date,  there have been no women presidents."

Then what are Vigdis Finnbogadottir, Mary Robinson and Tarja Halonen?
Men presidents who like to dress up as women? =)

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"It's not survival of the fattest, it's survival of the fittest."
   - Ludvig von Drake



Tue, 13 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings

posted the following:

Quote:
>: "To date" is used when something was and is still true.  Usually it's
>: used when there's an expectation that it will change in the future.
>: Such as, "To date,  there have been no women presidents."

>Then what are Vigdis Finnbogadottir, Mary Robinson and Tarja Halonen?
>Men presidents who like to dress up as women? =)

Ugh, American-centric viewpoint rears its ugly head again.  "To date,
there have been no American female presidents."

:)

-Chris



Tue, 13 Aug 2002 03:00:00 GMT  
 char[] vs char * vs #defines for constant strings
Joona I Palaste a crit dans le message

Quote:
>Then what are Vigdis Finnbogadottir, Mary Robinson and Tarja Halonen?
>Men presidents who like to dress up as women? =)

Ahh Mary...

***

--
-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



Tue, 13 Aug 2002 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. A char pointer (char *) vs. char array question

2. #define void int vs. #define void char

3. char **str vs. char *str[]

4. char *c vs. char c[]

5. char ** VS char *** ???

6. char [] vs char *

7. char ** vs char * f()

8. char * vs char[]

9. COM with ATL - char * vs unsigned char *

10. char *s vs char s[]

11. const char * vs char const *

12. arrays and pointers, char * vs. char [], distinction

 

 
Powered by phpBB® Forum Software