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"