> The whole typedef thing has always seemed a tad
> strange to me. Thankfully, you really haven't needed
> it for a few years.
'typedef' = "Type Define"
It never went out of style, it is valid today as always as a necessary part of the language. If you aren't using it, you are writing buggy or, at the least, non-portable code. No exceptions, period.
'typedef' is poorly understood by most because few are left today who even understand what's going on. I will try once again, since I seem to be the main champion and guardian of why how and what as regards structures...
do this instead:
If you just use 'struct', without the typedef, you are _NOT_ creating a variable and you are _NOT_ creating a type, you are defining a template. An amorphous thing to allow the compiler to calculate offsets during link. People fuzz the difference between declaration, definition, and type declaration and variable declaration.
typedef struct /* <- define type */
}bullet; /* <- declare type */
bullet bullets; /* declare variable of type 'bullet' */
People tend to think that because they can confuse a compiler's syntax engine and get code to compile without errors or warnings that the code is correct-- not necessarily so. A compiler is not infallable.
In development the 'end' does NOT justify the means. both are equally important and harmonious.
It always pays to do it right. Even if it seems tedious. If you use 'typedef' properly, it means you don't have to litter the 'struct' keyword throughout your code. Which happens to be a symptom of the compiler being 'not sure' what this is that you're referring to, so you have to keep reminding it the item in question is a 'struct'.
Do it right means the compiler handles error checking more accurately. It means portable, easier to maintain code. Why wouldn't you do it?
> Well, to accomplish it in the least amount of code, I'd do:
and justifying cutting corners to cut quantity of code is just plain lazy. In 98% of the cases, the amount of code you write has nothing whatsoever to do with how fast your application executes.
To answer your question-- how to make an array of structs any length you like, use malloc(). Using the above typedef, we can do this:
Since it's a single dimensioned array, it's easy to malloc() it and use it like any other array. Just remember to free() it when done.
bullet *listP; /* declare vars */
n = 5; /* initialize vars */
listP = 0L;
listP = (bullet*)malloc(10 * sizeof(bullet)); /* allocate an array of 10 bullets */
listP[n].bulletx = 5;
listP[n].bullety = 1;
. . .