Thread: New Kind of Typedef

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    New Kind of Typedef

    I have seen this type declaration. Can someone explain me this
    Code:
    typedef unsigned int uint16;
    typedef uint16                    Buf[32][8];
    Buf buf1;
    I slightly modified the variable names. I am confused and don't understand the Buf. Please advise.

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Seems to me to be just automatic stack arrays, for example
    Code:
    Buf a, b;
    a[31][7] = b[0][0];
    takes the last element of the last array in variable a and copies it to the first element of the first array in variable b

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    214
    @awwdert....I think that's actually copying the first element of the first array in b to the last element of the last array in a, but otherwise you're on point.

    I'm not sure what's "new" about this....is the ability to subscript the typedef a recent addition to the language?

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Whoops, Me dumb XP, anyway I think xey meant new as in new to xem

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cleaner123
    I am confused and don't understand the Buf. Please advise.
    It might be helpful to understand typedefs the way the standard sees them: a typedef is a declaration of identifiers that has the storage class specifier of typedef. So you should ask yourself, what does this declaration declare without the storage class specifier? That is, in this case, consider:
    Code:
    uint16 Buf[32][8];
    The answer of course is that Buf is an array of 32 arrays of 8 uint16 objects. Now consider the typedef storage class specifier, and Buf becomes the type of Buf without the typedef specifier, i.e., Buf is an alias of the type "array of 32 arrays of 8 uint16 objects". Hence, since Buf is used to declare buf1, buf1 is an array of 32 arrays of 16 uint16 objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. kind of new here and need help
    By yasharm in forum C Programming
    Replies: 7
    Last Post: 01-03-2007, 11:35 AM
  2. What kind of job...
    By Discolemonade in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-15-2005, 08:23 PM
  3. would anybody be kind enough to help?
    By almich in forum C++ Programming
    Replies: 10
    Last Post: 07-07-2004, 05:49 AM
  4. Getting three of a kind
    By Kons in forum C Programming
    Replies: 8
    Last Post: 08-19-2003, 12:44 PM
  5. anyone who is kind?
    By Christine in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2001, 02:55 PM

Tags for this Thread