How can I define a datatype with size constraint?

Discussion in 'C++' started by sandorf, Nov 9, 2005.

  1. sandorf

    sandorf Guest

    When I write:
    int a;
    and I want another name for "int", I can simply write
    typedef int interger

    but what if I want a more descriptive name for the datatype
    int a[2]; ?
    typedef int[2] two_intergers of course won't work.

    Thanks.
     
    sandorf, Nov 9, 2005
    #1
    1. Advertising

  2. sandorf wrote:
    > When I write:
    > int a;
    > and I want another name for "int", I can simply write
    > typedef int interger
    >
    > but what if I want a more descriptive name for the datatype
    > int a[2]; ?
    > typedef int[2] two_intergers of course won't work.


    typedef int two_integers[2];


    Jonathan
     
    Jonathan Mcdougall, Nov 9, 2005
    #2
    1. Advertising

  3. sandorf

    Sumit Rajan Guest

    "sandorf" <> wrote in message
    news:...
    > When I write:
    > int a;
    > and I want another name for "int", I can simply write
    > typedef int interger
    >
    > but what if I want a more descriptive name for the datatype
    > int a[2]; ?
    > typedef int[2] two_intergers of course won't work.


    typedef int two_ints[2];

    and you can then use it like this:
    two_ints two;

    Regards,
    Sumit.
    --
    Sumit Rajan <>
     
    Sumit Rajan, Nov 9, 2005
    #3
  4. sandorf wrote:
    > When I write:
    > int a;
    > and I want another name for "int", I can simply write
    > typedef int interger
    >
    > but what if I want a more descriptive name for the datatype
    > int a[2]; ?
    > typedef int[2] two_intergers of course won't work.


    What book are you reading? I am surprised that [if you're reading
    any book] it doesn't explain that you're supposed to do

    typedef int two_integers[2];

    V
     
    Victor Bazarov, Nov 9, 2005
    #4
  5. sandorf

    Marcus Kwok Guest

    sandorf <> wrote:
    > When I write:
    > int a;
    > and I want another name for "int", I can simply write
    > typedef int interger
    >
    > but what if I want a more descriptive name for the datatype
    > int a[2]; ?
    > typedef int[2] two_intergers of course won't work.


    typedef int two_integers[2];

    Also, note that they are "integers", not "intergers".

    --
    Marcus Kwok
     
    Marcus Kwok, Nov 9, 2005
    #5
  6. sandorf

    sandorf Guest

    oh, it works.
    thanks
     
    sandorf, Nov 9, 2005
    #6
  7. sandorf

    Guest

    sandorf wrote:
    > When I write:
    > int a;
    > and I want another name for "int", I can simply write
    > typedef int interger
    >
    > but what if I want a more descriptive name for the datatype
    > int a[2]; ?
    > typedef int[2] two_intergers of course won't work.
    >
    > Thanks.


    typedef int two_intergers[2];

    two_intergers var;
    var[0] = 100;
    var[1] = 200;

    .....
     
    , Nov 9, 2005
    #7
  8. sandorf

    Marcus Kwok Guest

    >Sumit Rajan <> wrote:
    >> typedef int two_ints[2];


    >> and you can then use it like this:
    >> two_ints two;


    sandorf <> wrote:
    > oh, it works.
    > thanks


    Please quote what you are following up to.

    An easy way to remember how it goes, is to think about how you would
    declare a variable of that type. For example, to declare an array of
    two ints, it is:

    int two_integers[2];

    When you do a typedef, place the new name where you would normally place
    the variable name, and put "typedef" at the beginning:

    typedef int two_integers[2];

    Remembering this can come in handy if you need to write complex typedefs
    for function pointers, etc.

    --
    Marcus Kwok
     
    Marcus Kwok, Nov 9, 2005
    #8
  9. sandorf

    Default User Guest

    wrote:

    >
    > sandorf wrote:
    > > When I write:
    > > int a;
    > > and I want another name for "int", I can simply write
    > > typedef int interger
    > >
    > > but what if I want a more descriptive name for the datatype
    > > int a[2]; ?
    > > typedef int[2] two_intergers of course won't work.
    > >
    > > Thanks.

    >
    > typedef int two_intergers[2];
    >
    > two_intergers var;
    > var[0] = 100;
    > var[1] = 200;



    This sort of usage (I know it's what the OP asked for) is considered by
    many to be very bad. It obfuscates the code for no particular advantage.

    About the only typedef that's useful is that of the function pointer,
    as those declarations are somewhat non-intuitive and can be easy to
    screw up.


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Nov 9, 2005
    #9
  10. sandorf

    Greg Comeau Guest

    In article <>,
    sandorf <> wrote:
    >When I write:
    >int a;
    >and I want another name for "int", I can simply write
    >typedef int interger
    >
    >but what if I want a more descriptive name for the datatype
    >int a[2]; ?
    >typedef int[2] two_intergers of course won't work.


    A typedef usually works like a declaration.
    Get one of them:

    int a[21];

    and then put typedef in front of it all:

    typedef int a[21];
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Nov 11, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WebdoM
    Replies:
    0
    Views:
    401
    WebdoM
    Nov 30, 2005
  2. Replies:
    1
    Views:
    502
    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
    Nov 27, 2006
  3. Rahul K

    Find the size of a datatype

    Rahul K, May 22, 2006, in forum: C Programming
    Replies:
    36
    Views:
    1,122
  4. Jeff
    Replies:
    2
    Views:
    1,197
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 16, 2007
  5. puvit82
    Replies:
    4
    Views:
    780
    puvit82
    Feb 1, 2008
Loading...

Share This Page