Questions tagged [typedef]

In C and C++, the typedef keyword allows you to create an alias for a known data type.

The typedef keyword can be followed by any C and C++ built-in data type, including int, short, signed, unsigned, char, signed char, unsigned char, double, long, or long double. In C++, the data type can also be an existing class that either is provided by one of the libraries that ship with the C++ compiler. For example, it can be the string class. The data type can also be a pointer to a known type.

2844 questions
1096
votes
13 answers

What is a typedef enum in Objective-C?

I don't think I fundamentally understand what an enum is, and when to use it. For example: typedef enum { kCircle, kRectangle, kOblateSpheroid } ShapeType; What is really being declared here?
Craig
  • 16,081
  • 11
  • 40
  • 39
1008
votes
7 answers

What is the difference between 'typedef' and 'using' in C++11?

I know that in C++11 we can now use using to write type alias, like typedefs: typedef int MyInt; Is, from what I understand, equivalent to: using MyInt = int; And that new syntax emerged from the effort to have a way to express "template…
Klaim
  • 60,771
  • 31
  • 121
  • 186
894
votes
12 answers

typedef struct vs struct definitions

I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference, they accomplish the same goal. struct myStruct{ …
user69514
  • 24,321
  • 56
  • 146
  • 183
879
votes
8 answers

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo { ... }; and: typedef struct { ... } Foo;
criddell
  • 13,189
  • 9
  • 39
  • 44
499
votes
6 answers

Typedef function pointer?

I'm learning how to dynamically load DLL's but what I don't understand is this line typedef void (*FunctionFunc)(); I have a few questions. If someone is able to answer them I would be grateful. Why is typedef used? The syntax looks odd; after…
Jack Harvin
  • 5,885
  • 7
  • 21
  • 21
447
votes
15 answers

Why should we typedef a struct so often in C?

I have seen many programs consisting of structures like the one below typedef struct { int i; char k; } elem; elem user; Why is it needed so often? Any specific reason or applicable area?
Manoj Doubts
  • 11,957
  • 15
  • 39
  • 42
398
votes
1 answer

C++ template typedef

I have a class template class Matrix { // .... }; I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that: typedef Matrix Vector; Which…
Notinlist
  • 14,748
  • 9
  • 51
  • 92
344
votes
10 answers

Equivalent of typedef in C#

Is there a typedef equivalent in C#, or someway to get some sort of similar behaviour? I've done some googling, but everywhere I look seems to be negative. Currently I have a situation similar to the following: class GenericClass { public…
Matthew Scharley
  • 115,776
  • 51
  • 189
  • 215
260
votes
12 answers

Is there a Java equivalent or methodology for the typedef keyword in C++?

Coming from a C and C++ background, I found judicious use of typedef to be incredibly helpful. Do you know of a way to achieve similar functionality in Java, whether that be a Java mechanism, pattern, or some other effective way you have used?
bn.
  • 7,031
  • 7
  • 37
  • 53
254
votes
10 answers

Forward declaration of a typedef in C++

Why won't the compiler let me forward declare a typedef? Assuming it's impossible, what's the best practice for keeping my inclusion tree small?
user96825
  • 2,541
  • 2
  • 15
  • 4
252
votes
7 answers

Understanding typedefs for function pointers in C

I have always been a bit stumped when I read other peoples' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around to such a definition while trying to understand a numerical algorithm…
user192712
249
votes
8 answers

uint8_t vs unsigned char

What is the advantage of using uint8_t over unsigned char in C? I know that on almost every system uint8_t is just a typedef for unsigned char, so why use it?
Lyndon White
  • 24,284
  • 15
  • 77
  • 126
229
votes
7 answers

typedef fixed length array

I have to define a 24-bit data type.I am using char[3] to represent the type. Can I typedef char[3] to type24? I tried it in a code sample. I put typedef char[3] type24; in my header file. The compiler did not complain about it. But when I defined a…
341008
  • 9,030
  • 10
  • 45
  • 84
193
votes
9 answers

Why do you use typedef when declaring an enum in C++?

I haven't written any C++ in years and now I'm trying to get back into it. I then ran across this and thought about giving up: typedef enum TokenType { blah1 = 0x00000000, blah2 = 0X01000000, blah3 = 0X02000000 } TokenType; What…
Tim Merrifield
  • 5,348
  • 5
  • 27
  • 32
187
votes
5 answers

How do I typedef a function pointer with the C++11 using syntax?

I'd like to write this typedef void (*FunctionPtr)(); using using. How would I do that?
rubenvb
  • 69,525
  • 30
  • 173
  • 306
1
2 3
99 100