typedef

Hi,

While looking at some code examples I saw the below

typedef char ContactField[0x100];

Does the above mean that ContactField is an array of chars? Could someone pls explain a bit more on this type of syntax.

Thanks in adavnce
typedef is used to create an alternative name for a type. After the typedef, ContactField can be used to mean an array of 256 char.
ContactField foo; and char foo[0x100]; will do the same thing.
Last edited on
Wow, I was completely unaware that you could typedef an array like that! That is awesome. I wonder why Windows.h doesn't have the following?

typedef TCHAR MaxPathCharArray[MAX_PATH];

That would be great.
I didn't know they used typedefs in windows.h
> I didn't know they used typedefs in windows.h

They use far too many of them. Including idiotic stuff like typedef void* PVOID ;
http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx
CHAR, FLOAT, INT, LONG, SHORT, VOID
¿why? ¿why there is no DOUBLE?
$MS programers are like scriptkiddies.

Jesus!, why type INT instead of int??
why VOID instead of void LOL

why don't they just do typedef typedef TYPEDEF lol ahahahh

API should be understandable but it seems they don't care about design.
they said Win32 API is hard to learn, O_O lol, of course it's hard since they screwed up everything with their ugly design so no-one can understand it except $MS kids.

$MS code is an UGLY CRAP.

they don't even respect C++ standard, they include headers into other headers in their flawor.
they don't support exception list which is standard from C++03.

tipical $MS:
you won't belive for what they use #define:
1
2
#define va_list va_list
#define va_start va_start 


now tell is that normal or sick??
A lot of what they do is to support legacy systems. Also, a lot of that stuff was never intended for C++. It's C so don't complain that they don't follow the latest c++ standard. It's made as generic as possible to make porting to other languages as simple as possible.

Sure you may not like that INT doesn't turn blue in your IDE like int but the meaning is just as clear.

Now typedef int INT may seem trivial, but they are abiding by a coding standard here. Not all systems and compilers treat int as 4 bytes, however INT must be 4 bytes for some things to work so they use a series of defines to ensure that it turns out this way. Consider the following from WinNT.h:
1
2
3
4
5
6
#ifndef _MAC
typedef wchar_t WCHAR;    // wc,   16-bit UNICODE character
#else
// some Macintosh compilers don't define wchar_t in a convenient location, or define it as a char
typedef unsigned short WCHAR;    // wc,   16-bit UNICODE character
#endif 

We change the type of WCHAR depending on the system that we are building this on. That's good design, not UGLY CRAP.

They didn't write Windows.h to be confusing, they wrote it to work, and they wrote it to work on any system from any era with any language. It does that and so they are successful.
Last edited on
#define VOID void and typedef void* PVOID ; are not just trivial, they are absurd.

While typedef int INT ; for a 32 bit integral type is not trivial, C's int32_t in <stdint.h> would have been a lot better - at least it says what it means.
@Stewbond
They didn't write Windows.h to be confusing, they wrote it to work, and they wrote it to work on any system from any era with any language

*BULLSHIT*

there are C++ API's and libs which support far more then Win32 API does and those does not look even close so ugly as Win32 API.

$MS API has serious problems with design which can not be redisigned any more. at least not so easy.

Well of course they look for compatibility and thats why all those typedef and #defines but anyway there are much better approaches, and $MS programers have chosed the most ugliest way.

OH yeah,
look at .NET framework design. all the mistakes maded in Win32 API and all that experience has ben transported to .NET, well they won't make a mistake one more time. look at .NET how polished is it now. no UGLY code at all. it's most beautiful API I've seen so far.(design I mean)

Coding standards. If you do:
1
2
3
typedef int INT;
typedef wchar_t WCHAR;
typedef long LONG;


Then why wouldn't you do the same with VOID?
you are saying typedef int INT so you can be shore that INT will allways be 4 Bytes.

well why does then exist short and long?
short is allways 2B while long is allways 4B isn it? (correct me if not)
you don't have to use int at all. use short and long and forget about int. no typedef needed.


Coding standards. If you do:

1
2
3
typedef int INT;
typedef wchar_t WCHAR;
typedef long LONG;


Then why wouldn't you do the same with VOID?


well better option would be delete typedefs and stick with C++ standard as most non-$MS programers do :D
Last edited on
True int can be different with different configurations. That's what we are using the typedef for. While typedef int INT may not be the entire solution, having a few options which are seperated with some #ifdef statements will help us to select what INT will be. typedef int INT is just one (and probably the most common) possibility.
codekiddy wrote:
stick with C++ standard as most non-$MS programers do

No. As I said, the windows headers are meant to be GENERIC. They are VERY commonly used with C and are often ported to other languages. Putting in c++ specific code would destroy this functionality.

The people who wrote those headers and the Win API are very good programmers, they don't just add complexity without reason. Just because you may not need 99% of the stuff they write doesn't mean that other's don't.
hehe, I see we are picking on Microsoft now.

Microsoft did what it did to have full control on their data types. If in the future an int changes size in some compiler, they can update the SDK so INT points to the right 4-byte int in that specific compiler. Same for all other data types. I know, the C++ standard ensures the size of certain data types, but still the point is valid.

It is not poor design, it is ultimate indirection. It is very clever, if you ask me. Most of us don't have such a far-reaching vision.

In any case, that's just my opinion. Flame it away if you must. :-P
Topic archived. No new replies allowed.