typedef for arrays?

This is a discussion on typedef for arrays? within the C++ Programming forums, part of the General Programming Boards category; Hi, I would like to introduce a new type for something like that Code: char name[32]; to avoid inconsistence over ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    typedef for arrays?

    Hi,

    I would like to introduce a new type for something like that

    Code:
    char name[32];
    to avoid inconsistence over the project (in case the same array size is needed somethere else)

    Code:
    typedef NAME_T char[32] // doesn't work
    NAME_T name;
    Is this possible somehow for arrays?

    Thank you!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Make a header file?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Why do you want to, especially for native arrays when you can use std::vector?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about
    Code:
    typedef char NAME_T[32];
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Todd Burch View Post
    Make a header file?
    It is. But how would that help me avoiding to rely on that nobody changes that 32 to 64 for example (my module would break in that case if it has the 32 hardcoded inside)?



    Why do you want to, especially for native arrays when you can use std::vector?
    Hm, it's
    Code:
    class ...
    {
      NAME_T name;
    
      ctor()
      {
         //copy to name
      }
    
    };
    
    versus
    
    class ...
    {
     std::vector<char> name;
    
      ctor()
      {
         name.resize(name_len);
         if(0 < name_len)
         {
            //copy to &name[0];
         }
      }
    };
    more writing for what advance? ok, maybe the getter for name could return by value instead of const char*...

    typedef char NAME_T[32];
    That works!

    Thank you all so far!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Why do you want to, especially for native arrays when you can use std::vector?
    or std::string.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Native arrays are perfectly fine for some tasks.

    I wouldn't define a typedef for arrays, though. I'd define a constant for the size and simply use that everywhere.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21