Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: TC01 on September 21, 2010, 06:03:53 pm

Title: Variable length strings in structures?
Post by: TC01 on September 21, 2010, 06:03:53 pm
So- here's a C language question that reflects my lack of understanding of C- is it possible to define a variable-length string (aka character array) in a structure? (Specifically, in the GNU C implementation used by TIGCC/GCC4TI).

That's the basic question- here's why I want to know:

I have this structure defined in Solar89 (Comments should describe what each item is for):

Code: [Select]
typedef struct
{
    size_t chars; // How many chars are in the string making up the token
    int twoByteToken; // Whether or not the token is two bytes (0 or 1)
    unsigned char hex[2]; // The two bytes making up the token, or only hex[0] if it's a one-byte token
    char name[14]; // The name of the token (never more than 14 chars!)
} tokenInfo;

The way Solar89 currently works is that it reads in a text file and creates an array of these structures. This allows an editable database to store tokens, but it means the initialization will be slow.

I decided I could make it faster by making a second program that does the same thing and writes that array directly to a file. In theory (and correct me if I'm wrong- if I am this entire question is irrelevant) I could read the tokens in much faster by just doing something this (with size being the length of the file, or something like that):

Code: [Select]
fread(tokens, size, sizeof(tokenInfo), file);
Well, I tested the "token file compiler". The token file outputted is about twice the size as the text file is- for a 5000 byte file, it's around 11000 bytes. Now, this might be worth it if it made the process much faster- I haven't tested that though.

I suspect I could get rid of some space if I could shrink down tokenInfo.name when the token name was only "A" or "1" or "prgm"... etc. But I don't know how to do that.

Is this clear? If not, please just go back to the first paragraph for the actual question and try to ignore the context.
Title: Re: Variable length strings in structures?
Post by: TC01 on September 27, 2010, 08:00:56 am
Never mind, I realized:

a. How easy this is to do, and:
b. How it won't work for what I want (I can't get a program on the other end to unpack a file containing variable length strings...)

I feel silly now.
Title: Re: Variable length strings in structures?
Post by: DJ Omnimaga on September 27, 2010, 11:09:18 am
I wish I could be able to help, though. I feel a bit sad that there were no reply at all x.x
Title: Re: Variable length strings in structures?
Post by: apcalc on September 27, 2010, 03:42:02 pm
I feel a bit sad that there were no reply at all x.x

I completely missed this topic. :(

Sorry I could not help. :(