Jul 072008

Let’s suppose you wanna create a typedef for a fixed length array, so first thing that you would do is this…

typedef char[100] HundredChars;

This will not work as typedefing an array requires different syntax, here is how we do it…

typedef char HundredChars[100];

  8 Responses to “typedef a fixed length array”

  1. 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++?

  2. thanks for your examples

  3. […] typedef a fixed length array July 2008 4 comments […]

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

  5. Yeah hope so! I guess this is the standard syntax.

  6. Will this comply across all compilers?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.