TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Len Philpot
date: 2004-01-25 20:15:30
subject: Re: [C] Question about external variable initialization

From: "Len Philpot" 

>  LP>         ext_char_arr[2] = "element two";
>
> Where do you malloc() those?

I don't - See below for my probaly flawed reasoning...


> .. And you can't assign a string value in that way, you should use
>
> strcpy(destination var, source var), so it should be,
>
> strcpy(ext_char_arr[0], "element zero");

Well... I understand what you're saying, and apparently I've missed
something (doh!) in my understanding of this. I'm aware that I can't, e.g.,

char text[12];
text = "Some text";

Instead, I have to strcpy or similar to assign it. What I was thinking that
when I create a pointer to char, I can effectively point it at a string
literal I've created. I thought my process here was : 1) The compiler will
allocate memory for string literals, then 2) Simply assign each char
pointer in ext_char_arr with the address of the first character of the
(null-terminated) string literal being created - Effectively an array of
strings.

Have I missed the boat?

I guess to do what I'm describing would need something like :

char text[] = "This is some text";
char *ptext;
ptext = &text[0];
/* or same thing : ptext = text; */

...although I'm not sure why I'd want to do that.


> You can also use ext_char_arr[0] = strdup("This is a text");

Just as an aside, how portable is strdup()?


>  LP> What precipitated this was a workalike of the 'banner'
>  LP> program I wrote (called "pennant", naturally). The
>  LP> program is about 50 lines of code and 800+ of the large
>  LP> "character" definitions. Obviously it would be nice to
>  LP> get those definitions in their own file.
>
> If you have a unknown number of lines, you could use char**, or a linked
> list (the linked list might be easier).

Understood - Once long ago, I used ** in a program that displayed a
relatively large text file (4MB+), jumping to specific sections of it, but
offhand right now I can't recall why... I guess I need to either fish or
cut bait with C! :-)

Back to your recommendation, for example, the main file buffer in a text
editor might be a double linked list, right? However in this case, I know
exactly how much data there is - The large "character set", and
it won't change (usually famous last words, but I think it's true here :-)
In the case of my banner workalike, it is a 760 element array, each group
of eight 8-character elements comprising one large character. Hence my
desire to move 760 lines of initialization out of the main file.

Thanks for the reply.

--
 -- Len Philpot -> len{at}philpot.org     <--
 ------ > -----> http://philpot.org/ <--

--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)
SEEN-BY: 633/267 270
@PATH: 261/38 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.