TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Len Philpot
from: Bo Simonsen
date: 2004-01-27 15:42:00
subject: Re: [C] Question about external variable initialization

LP> From: "Len Philpot" 

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

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

Yes I saw.

> .. 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");

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

Then you got a char* it's a address! Well I can say.

char* test = NULL;

Then test is pointing to null.

If I do:

char* test = malloc(7);

I asked the OS to allocate 7 bytes, and it returns the address to them.

I can do:

int a = 5;

int* b = &a;

I set pointer b as the address to a.

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

 LP> Have I missed the boat?

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

 LP> char text[] = "This is some text";
 LP> char *ptext;
 LP> ptext = &text[0];

Correctly.

 LP> /* or same thing : ptext = text; */

Correct!

 LP> ...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");

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

Should be very portable, else you can make it:

/* Precondition: A pointer to chars
   Postcondition: A pointer which is pointed to a copy of 'precondition' 
                  if !precondtion NULL is returned
   Invariant: Precondition != NULL
*/

char* strdup(char* text)
{
        char* tmptext = NULL;

        if(text) /* if text is not NULL */
        {
                tmptext = malloc(strlen(text)+1); 
                /* allokate the lenght of text and 1 extra char for the 
                   string NULL termination */
                strcpy(tmptext, text);
        }

        return tmptext;
}


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

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

For normal strings a char** would be suitable else you could do a :

typedef struct TextBlock_
{
        char text[128];
        struct TextBlock_* next;
} TextBlock;

TextBlock* first = NULL;

void AddToList(char* text)
{
        TextBlock* tmp = NULL;

        tmp = malloc(sizeof(TextBlock)); /* alloked 134 (128+4) */
        strcpy(tmp->text, text);

        tmp->next = firsT;
        first = tmp;

        return;
}

 LP> Back to your recommendation, for example, the main file 
 LP> buffer in a text editor might be a double linked list, 
 LP> right? 

Yes that might be suitable or a char** where the index is the line.

 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> Thanks for the reply.

You're welcome.

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