TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Len Philpot
from: Bo Simonsen
date: 2004-01-28 08:39:06
subject: Re: [C] Question about external variable initialization

LP> From: "Len Philpot" 

Hi Len,

>  LP> char text[12];
>  LP> text = "Some text";
>
> Hmm.. I doubt it would work, but I can't explain why, because you may
> initialize a array in that way.

 LP> It _shouldn't_, since the name of the 'string' (as a 
 LP> character array) is really just a pointer to the first 
 LP> element of the array. I don't know of any way to assign 
 LP> a string of text to a pointer except by defining and 
 LP> initializing it at the same time (char 
 LP> *text="text";)... but then again, there's a lot I don't 
 LP> know.

Yes, but you may initalise a _array_ by char test[] = "Hej\0";

Let agree about char[] is not the same as char* :)

char[] is doing a array on the heap from start, while a pointer is just a
random address by default.


>  LP> Just as an aside, how portable is strdup()?
>
> Should be very portable, else you can make it:

 LP> Just curious - Although I see it here and there on the 
 LP> Single Unix Specification (and PC compilers), it's 
 LP> apparently not a part of ISO C.

I've it on Linux too.

>  LP> However in this case, I know exactly how much
>  LP> data there is - The large "character set", and it won't
>  LP> change (usually famous last words, but I think it's
>  LP> true here :-) In the case of my banner workalike, it is
>  LP> a 760 element array, each group of eight 8-character
>  LP> elements comprising one large character. Hence my
>  LP> desire to move 760 lines of initialization out of the main file.
>
> Hmm.. I'm not sure I understand.

 LP> For example :
 LP>         char *chr[] = {
 LP>                 " ###### ",
 LP>                 " ###### ",
 LP>                 "   ##   ",
 LP>                 "   ##   ",
 LP>                 "   ##   ",
 LP>                 "   ##   ",
 LP>                 "   ##   ",
 LP>                 "        " };

 LP> ...and so on, except there are a total of 760 elements, 

Aha, then you are doing the same mistake because this is equlivent to:

char* test[x];

test[0] = "fflflflf"

Which is a problem use, char[][] = { "blllaa", "bllaaa" };

 LP> for the other 94 characters banner supports (or at 
 LP> least did in the implementation I used to create my 
 LP> characters). A formula figures the offset into the 
 LP> array for a given character and the entire string is 
 LP> displayed one "pixel" at a time (cols x rows). As you 
 LP> can guess, it takes up a fair amount of file space to 
 LP> assign all these values.

If I should use a banner I would just create a file or so, and then
read it and display it, requires less memory, or do a for-loop

 LP> I've since come up with a scheme to use an integer array (I guess it 
 LP> _could_ be unsigned char) where each element contains a 
 LP> bitmask value between 0 and 255 of the elements as 
 LP> shown above. A function converts the value to a string 
 LP> and displays it. Makes the initialization of the array 
 LP> take up less space and no pointers to dink with... :-) 
 LP> Although since I'm doing this for my own edification I 
 LP> should use pointers _everywhere_ I can just to immerse 
 LP> myself in them... I understand the /basic/ concepts, 
 LP> but between giant lapses in contact with C and other 
 LP> missing mental functionality , I tend to overlook 
 LP> and misunderstand things more often that I'd like.

typedef unsigned char byte; :)

Bo


--- Maximus/UNIX 3.03b
* Origin: The Night Express - Roennede, Dk (2:236/100)
SEEN-BY: 633/267 270
@PATH: 236/100 237/9 20/11 106/1 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™.