Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Constant Strings

Reply
Thread Tools

Constant Strings

 
 
Adam L.
Guest
Posts: n/a
 
      08-30-2007
Hello all, again.

It's the Pascal guy trying to figure stuff out in C.

One of my programming 'ways' in Pascal is to create a unit file that has
most of the program's strings. Error messages, window titles, file paths,
etc... These are all constants.

1) What is the best way to have a long list of constant strings in C? I
read somewhere that I shouldn't define variables in a header file (which I
would do in the Interface part of a Pascal Unit). Do I just make some .c
file with all the strings and #include it somewhere?

2) What would you recommend as the type? A #define, const char[], ?

Just a note - I'm not using any type of RAD environment. This is a Linux
project. Much like my FreePascal coding, I use NEdit and the compiler. So
I don't have a fancy resource editor to click on.

Thanks!


--


My e-mail address is not that colorful.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-30-2007
Adam L. wrote:
> Hello all, again.
>
> It's the Pascal guy trying to figure stuff out in C.
>
> One of my programming 'ways' in Pascal is to create a unit file that has
> most of the program's strings. Error messages, window titles, file paths,
> etc... These are all constants.
>
> 1) What is the best way to have a long list of constant strings in C? I
> read somewhere that I shouldn't define variables in a header file (which I
> would do in the Interface part of a Pascal Unit). Do I just make some .c
> file with all the strings and #include it somewhere?
>
> 2) What would you recommend as the type? A #define, const char[], ?
>

I would declare them as "extern const char*" in a header and define them
in source module.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      08-30-2007
On Aug 30, 5:02 pm, "Adam L." <REDantituB...@BLACKmsbx.netGREEN>
wrote:
> One of my programming 'ways' in Pascal is to create a unit file that has
> most of the program's strings. Error messages, window titles, file paths,
> etc... These are all constants.
>
> 1) What is the best way to have a long list of constant strings in C?


char const *const strings[] =
{ "string1"
, "string2"
, "the next string"
};

Then in your header file you can make this accessible to the
outside world by writing:
extern char const * const strings[];

If you want some bounds checking you'll have to do that
expliclitly, e.g. include a:
#define NUM_STRINGS 10
in the header file, and then perhaps a static assert in
the source file to check you actually have enough strings.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-30-2007
Adam L. said:

<snip>

> 1) What is the best way to have a long list of constant strings in C?


"Best" is a somewhat nebulous term.

One very simple way is to encapsulate the strings in a function:

const char *GetErrorString(size_t idx)
{
const char *ma[] =
{
"OK",
"Not enough wings for sodium substrate",
"Hub light has fallen out",
"Grain is too soft",
"Ink leak in cowshed",
"Microfilter is the wrong colour"
};
size_t len = sizeof ma / sizeof ma[0];
return idx < len ? ma[idx] : NULL;
}

You might wish to consider making the message array static.

> (which I
> would do in the Interface part of a Pascal Unit). Do I just make some
> .c file with all the strings and #include it somewhere?


When you understand all eight translation phases, you will be in a good
position to realise not only why it's generally a bad idea to #include
..c files, but also why on very rare occasions it can be a good idea.

This is not one of those very rare occasions.

> Just a note - I'm not using any type of RAD environment. This is a
> Linux project.


Linux /is/ a RAD environment.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-30-2007
"Adam L." wrote:
>
> It's the Pascal guy trying to figure stuff out in C.
>
> One of my programming 'ways' in Pascal is to create a unit file
> that has most of the program's strings. Error messages, window
> titles, file paths, etc... These are all constants.


How are you going to refer to them? If by index number (possibly
something enumerated) you have to create an array of pointers and
initialize that. That can be a const array, with the pointers
pointing to const strings. Put it in a .c file, and make a .h file
that provides the essential information to other compilation units.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      08-30-2007
CBFalconer wrote:

> "Adam L." wrote:
>>
>> It's the Pascal guy trying to figure stuff out in C.
>>
>> One of my programming 'ways' in Pascal is to create a unit file
>> that has most of the program's strings. Error messages, window
>> titles, file paths, etc... These are all constants.

>
> How are you going to refer to them? If by index number (possibly
> something enumerated) you have to create an array of pointers and
> initialize that. That can be a const array, with the pointers
> pointing to const strings. Put it in a .c file, and make a .h file
> that provides the essential information to other compilation units.


And do that latter /with a program/, to avoid horrible didn't-match-up
and didn't-recompile-everything errors.

--
Chris "automatic" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-30-2007
On 2007-08-30 05:10, Ian Collins <ian-> wrote:
> Adam L. wrote:
>> One of my programming 'ways' in Pascal is to create a unit file that has
>> most of the program's strings. Error messages, window titles, file paths,
>> etc... These are all constants.
>>
>> 1) What is the best way to have a long list of constant strings in C? I
>> read somewhere that I shouldn't define variables in a header file (which I
>> would do in the Interface part of a Pascal Unit). Do I just make some .c
>> file with all the strings and #include it somewhere?
>>
>> 2) What would you recommend as the type? A #define, const char[], ?
>>

> I would declare them as "extern const char*" in a header and define them
> in source module.


Why "extern const char*" and not "extern const char[]"?

(Yes, I can think of a reason - you don't have to change the interface
if in the future you decide that the strings shouldn't really be
constant)

hp


--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-30-2007
"Adam L." <> writes:

> One of my programming 'ways' in Pascal is to create a unit file that has
> most of the program's strings. Error messages, window titles, file paths,
> etc... These are all constants.
>
> 1) What is the best way to have a long list of constant strings in C? I
> read somewhere that I shouldn't define variables in a header file (which I
> would do in the Interface part of a Pascal Unit). Do I just make some .c
> file with all the strings and #include it somewhere?


As has been said, "best" is rather hard to pin down. If you want to
name your strings (rather than having them indexed) you can do it like
this:

#ifndef H_MYSTRINGS
#define H_MYSTRINGS

#ifdef DEFINE_THE_STRINGS
#define MY_STRING(n, s) const char n[] = s
#else
#define MY_STRING(n, s) extern const char n[]
#endif

MY_STRING(error_one, "input required");
MY_STRING(error_two, "no input expected");
#endif

You put this in, say, "mystrings.h" and include it in any .c files
that use strings. One, and only one, .c file will have this code:

#define DEFINE_THE_STRINGS
#include "mystrings.h"

causing the const char arrays to be defined and initialised.

You can reduce the problem of having so many global names by using
token pasting to add a prefix to them all, if you like:

#define MY_STRING(n, s) const char msg_##n[] = s

I'll add an observation. This only pays off if these strings are used
all over the place, and such programs are not common. If you find
that you are referring to shared strings in lots of places, you may
want to think about some other design.

For example, error messages are often better handled by codes, with
only one function that needs to know how to turn them into strings.
In that case the, strings will just be in a table inside (or "close
to") the error function.

File names are usually best coming from outside of the program. They
should be set in configuration files or supplied as command-line
arguments. A few default names might be wanted, but it is likely that
they will be all in once place and the more usual static declaration
or simple a #define will suffice.

To put it simply, not all strings are created alike, and collecting
them together because they are strings may not be the right pattern.

--
Ben.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-30-2007
"Peter J. Holzer" wrote:
> Ian Collins <ian-> wrote:
>> Adam L. wrote:
>>
>>> One of my programming 'ways' in Pascal is to create a unit file
>>> that has most of the program's strings. Error messages, window
>>> titles, file paths, etc... These are all constants.
>>>
>>> 1) What is the best way to have a long list of constant strings
>>> in C? I read somewhere that I shouldn't define variables in a
>>> header file (which I would do in the Interface part of a Pascal
>>> Unit). Do I just make some .c file with all the strings and
>>> #include it somewhere?
>>>
>>> 2) What would you recommend as the type? A #define, const
>>> char[], ?

>>
>> I would declare them as "extern const char*" in a header and
>> define them in source module.

>
> Why "extern const char*" and not "extern const char[]"?
>
> (Yes, I can think of a reason - you don't have to change the
> interface if in the future you decide that the strings shouldn't
> really be constant)


And I can think of an anti-reason. You don't want to generate the
extra coding to copy all those strings into their storage in the
first place. The code generated is not proportional in size to the
source text.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-31-2007
On 2007-08-30 20:45, CBFalconer <> wrote:
> "Peter J. Holzer" wrote:
>> Ian Collins <ian-> wrote:
>>> Adam L. wrote:
>>>> One of my programming 'ways' in Pascal is to create a unit file
>>>> that has most of the program's strings. Error messages, window
>>>> titles, file paths, etc... These are all constants.
>>>>
>>>> 1) What is the best way to have a long list of constant strings
>>>> in C? I read somewhere that I shouldn't define variables in a
>>>> header file (which I would do in the Interface part of a Pascal
>>>> Unit). Do I just make some .c file with all the strings and
>>>> #include it somewhere?
>>>>
>>>> 2) What would you recommend as the type? A #define, const
>>>> char[], ?
>>>
>>> I would declare them as "extern const char*" in a header and
>>> define them in source module.

>>
>> Why "extern const char*" and not "extern const char[]"?
>>
>> (Yes, I can think of a reason - you don't have to change the
>> interface if in the future you decide that the strings shouldn't
>> really be constant)

>
> And I can think of an anti-reason. You don't want to generate the
> extra coding to copy all those strings into their storage in the
> first place.


Which extra coding? I was assuming that Ian meant something like this:

const char *msg1 = "Hello, world";
const char *msg2 = "How are your nasal demons?";
....

and asked why he preferred that to this:

const char msg1[] = "Hello, world";
const char msg2[] = "How are your nasal demons?";
....

In both cases there is no code here which copies anything. The linker
produces a suitable data segment in the executable, which is loaded at
startup.

(The possible change I was hinting at was that if msg1 is a pointer, you
can do something like

fgets(s, sizeof(s), msg_catalog_fp);
msg1 = mystrdup(s);

in an initialization routine and the rest of the application won't
notice any change - that is extra code, of course, but I mentioned that
as a future option)

hp


--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57