Let’s suppose you wanna create a typedef for a fixed length array, so first thing that you would do is this…
[sourcecode language=’cpp’]typedef char[100] HundredChars;[/sourcecode]
This will not work as typedefing an array requires different syntax, here is how we do it…
[sourcecode language=’cpp’]typedef char HundredChars[100];[/sourcecode]
Will this comply across all compilers?
Yeah hope so! I guess this is the standard syntax.
shouldn’t this be called HundredAndOneChars? ;p
🙂
No, it shouldn’t. If I declare:
HundredChars myVar;
Then myVar may be indexed myVar[0] to myVar[99]. 100 chars. When treated as a string, the maximum number of useful characters would be 99 ( myVar[0] .. myVar[98] with a null terminator at myVar[99] = ”. But it doesn’t have to ever be used as a string, it could always be used as a character array in which case all 100 spots could hold a useful character. Think about a class with 100 students.
HundredChars studentGrades;
could hold a letter grade for 100 students numbered 0 through 99.
Pingback: 2010 in review « bits and bytes
thanks for your examples
If I try to make a function returning HundredChars
compiler refuses (cannot have function return an array).
This largely defeats the joy of being able to define HundredChars as a “type.”
I really want to be able to return short fixed length arrays from functions, and I do not
want to have to pretend they are structs, I want them to be arrays, dammit.
How? Can I at least do this in C++?