Author Topic: Variable length strings in structures?  (Read 3563 times)

0 Members and 1 Guest are viewing this topic.

Offline TC01

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 344
  • Rating: +9/-0
    • View Profile
Variable length strings in structures?
« 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.



The userbars in my sig are links embedded links.

And in addition to calculator (and Python!) stuff, I mod Civilization 4 (frequently with Python).

Offline TC01

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 344
  • Rating: +9/-0
    • View Profile
Re: Variable length strings in structures?
« Reply #1 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.



The userbars in my sig are links embedded links.

And in addition to calculator (and Python!) stuff, I mod Civilization 4 (frequently with Python).

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Variable length strings in structures?
« Reply #2 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

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Variable length strings in structures?
« Reply #3 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. :(