Questions tagged [typedef]

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

typedef
Filter by
Sorted by
Tagged with
1259 votes
8 answers
448k views

What is the difference between 'typedef' and 'using'?

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 ...
Klaim's user avatar
  • 68.4k
1111 votes
13 answers
468k views

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 ...
Craig's user avatar
  • 16.3k
1034 votes
12 answers
1.0m views

typedef struct vs struct definitions [duplicate]

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, ...
user69514's user avatar
  • 27.4k
966 votes
8 answers
661k views

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

In C++, is there any difference between: struct Foo { ... }; and: typedef struct { ... } Foo;
criddell's user avatar
  • 14.5k
570 votes
6 answers
605k views

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. ...
Jack Harvin's user avatar
  • 6,705
548 votes
15 answers
663k views

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's user avatar
  • 13.8k
451 votes
1 answer
232k views

C++ template typedef

I have a class template<size_t N, size_t M> 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. ...
Notinlist's user avatar
  • 16.4k
392 votes
13 answers
271k views

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 ...
Matthew Scharley's user avatar
295 votes
8 answers
400k views

What is the advantage of uint8_t over 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?
Frames Catherine White's user avatar
287 votes
12 answers
143k views

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,...
bn.'s user avatar
  • 7,829
285 votes
8 answers
337k views

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 ...
user avatar
282 votes
11 answers
211k views

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's user avatar
  • 2,821
257 votes
7 answers
292k views

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 ...
341008's user avatar
  • 10k
236 votes
5 answers
97k views

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's user avatar
  • 75.6k
222 votes
9 answers
241k views

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 = ...
Tim Merrifield's user avatar
204 votes
9 answers
107k views

Internal typedefs in C++ - good style or bad style? [closed]

Something I have found myself doing often lately is declaring typedefs relevant to a particular class inside that class, i.e. class Lorem { typedef boost::shared_ptr<Lorem> ptr; typedef ...
Will Baker's user avatar
  • 2,795
163 votes
9 answers
150k views

self referential struct definition?

I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error ...
Ziggy's user avatar
  • 22.1k
152 votes
11 answers
81k views

Are typedef and #define the same in C?

I wonder if typedef and #define are the same in C. What are the differences between them?
user avatar
150 votes
22 answers
145k views

Convert objective-c typedef to its string equivalent

Assuming that I have a typedef declared in my .h file as such: typedef enum { JSON, XML, Atom, RSS } FormatType; I would like to build a function that converts the numeric value of the ...
craig's user avatar
  • 26k
148 votes
4 answers
125k views

Which header should I include for size_t?

According to cppreference.com size_t is defined in several headers, namely <cstddef> <cstdio> <cstring> <ctime> And, since C++11, also in <cstdlib> <cwchar> ...
463035818_is_not_an_ai's user avatar
101 votes
7 answers
59k views

What are function typedefs / function-type aliases in Dart?

I have read the description, and I understand that it is a function-type alias. A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types....
Gero's user avatar
  • 13.3k
100 votes
1 answer
4k views

What's the purpose of this [1] at the end of struct declaration?

I was snooping through my MSP430 microcontroller's header files, and I ran into this in <setjmp.h>: /* r3 does not have to be saved */ typedef struct { uint32_t __j_pc; /* return address */ ...
Alexander's user avatar
  • 61.5k
96 votes
15 answers
50k views

Is it a good idea to typedef pointers?

I looked through some code and noticed that the convention was to turn pointer types like SomeStruct* into typedef SomeStruct* pSomeStruct; Is there any merit to this?
Unknown's user avatar
  • 46.3k
91 votes
2 answers
44k views

How do I declare typedef in Swift

If I require a custom type in Swift, that I could typedef, how do I do it? (Something like a closure syntax typedef)
esh's user avatar
  • 2,862
82 votes
2 answers
77k views

How to typedef a template class? [duplicate]

How should I typedef a template class ? Something like: typedef std::vector myVector; // <--- compiler error I know of 2 ways: (1) #define myVector std::vector // not so good (2) template<...
iammilind's user avatar
  • 69.1k
82 votes
6 answers
40k views

Forward-declare enum in Objective-C

I'm having trouble with enum visibility in an Objective-C program. I have two header files, and one defines a typedef enum. Another file needs to use the typedef'd type. In straight C, I would simply ...
Stephen Touset's user avatar
81 votes
5 answers
17k views

C++ static polymorphism (CRTP) and using typedefs from derived classes

I read the Wikipedia article about the curiously recurring template pattern in C++ for doing static (read: compile-time) polymorphism. I wanted to generalize it so that I could change the return types ...
Samuel Powell's user avatar
80 votes
13 answers
66k views

When should I use typedef in C++?

In my years of C++ (MFC) programming in I never felt the need to use typedef, so I don't really know what is it used for. Where should I use it? Are there any real situations where the use of typedef ...
djeidot's user avatar
  • 4,582
77 votes
5 answers
4k views

What does this typedef statement mean?

In a C++ reference page they provide some typedef examples and I'm trying to understand what they mean. // simple typedef typedef unsigned long mylong; // more complicated typedef typedef int int_t, ...
rsgmon's user avatar
  • 1,902
77 votes
2 answers
58k views

C++ - meaning of a statement combining typedef and typename [duplicate]

In a C++ header file, I am seeing this code: typedef typename _Mybase::value_type value_type; Now, as I understand, quoting from « C++ the Complete Reference » by Schildt. typename can be ...
Arvind's user avatar
  • 6,434
73 votes
6 answers
71k views

C : typedef struct name {...}; VS typedef struct{...} name;

As the title says, I have this code: typedef struct Book{ int id; char title[256]; char summary[2048]; int numberOfAuthors; struct Author *authors; }; ...
Alek Sobczyk's user avatar
73 votes
7 answers
34k views

Propagating 'typedef' from based to derived class for 'template'

I'm trying to define base class, which contains typedef's only. template<typename T> class A { public: typedef std::vector<T> Vec_t; }; template<typename T> class B : public A&...
dimba's user avatar
  • 27.2k
71 votes
7 answers
273k views

dereferencing pointer to incomplete type

I've seen a lot of questions on this but I'm going to ask the question differently without specific code. Is there a way of EASILY determining what is causing the type to be incomplete? In my case I'...
nick's user avatar
  • 1,055
69 votes
3 answers
34k views

How to extend a typedef parameter in JSDOC?

Assuming you have the following code inside a ES6 class (documentation): /** * @typedef Test~options * @type {object.<string>} * @property {array} elements - An array containing elements * @...
dude's user avatar
  • 5,856
68 votes
4 answers
62k views

What's the syntax for declaring an array of function pointers without using a separate typedef?

Arrays of function pointers can be created like so: typedef void(*FunctionPointer)(); FunctionPointer functionPointers[] = {/* Stuff here */}; What is the syntax for creating a function pointer ...
Maxpm's user avatar
  • 24.8k
68 votes
1 answer
12k views

Destructors of builtin types (int, char etc..)

In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happens: ...
Nils Pipenbrinck's user avatar
63 votes
4 answers
50k views

Opaque C structs: various ways to declare them

I've seen both of the following two styles of declaring opaque types in C APIs. What are the various ways to declare opaque structs/pointers in C? Is there any clear advantage to using one style over ...
splicer's user avatar
  • 5,374
62 votes
3 answers
280k views

How do I use typedef and typedef enum in C?

Consider: #define MAXROW 20 #define MAXCOL 60 typedef State Grid[MAXROW+2] [MAXCOL+2] typedef enum state {DEAD,ALIVE} State How do I use typedef and typedef enum in C? What does this part of the ...
user39555's user avatar
  • 719
62 votes
8 answers
102k views

What is the use of typedef?

What is the use of typedef keyword in C ? When is it needed?
hks's user avatar
  • 649
60 votes
5 answers
114k views

invalid use of incomplete type

I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below. Does anyone know where I'm going wrong? template<typename Subclass> class A { ...
seanhodges's user avatar
  • 17.4k
57 votes
5 answers
78k views

What is 'forward declaration' and the difference between 'typedef struct X' and 'struct X'?

I am a beginner in C programming and I know the difference between struct type declaration and typedef struct declaration. I came across to know an answer saying that if we define a struct like: ...
r_goyal's user avatar
  • 1,147
56 votes
5 answers
66k views

What does "typedef void (*Something)()" mean

I am trying to understand what this means, the code I am looking at has in .h typedef void (*MCB)(); static MCB m_process; in .C MCB Modes::m_process = NULL; And sometimes when I do m_process(); ...
DogDog's user avatar
  • 4,900
56 votes
2 answers
1k views

A confusing typedef involves class scope

I'm reading code of a C++ project and it contains some code of the following form: namespace ns { class A {}; class B {}; } struct C { typedef ns::A* ns::B::* type; }; Can someone ...
Tien's user avatar
  • 583
55 votes
5 answers
4k views

Can I mimic a C header that redefines bool in C++?

I am writing a program and I would really prefer to write in C++, however, I'm required to include a C header that redefines bool: # define false 0 # define true 1 typedef int bool; The obvious ...
L Co's user avatar
  • 845
53 votes
6 answers
19k views

If I do a `typedef` in C or C++, when should I add `_t` at the end of typedef'ed type? [duplicate]

I am confused when should I add the trailing _t to typedef'ed types? For example, should I do this: typedef struct image image_t; or this: typedef struct image image; What are the general rules? ...
bodacydo's user avatar
  • 77.4k
53 votes
5 answers
35k views

Header file best practices for typedefs

I'm using shared_ptr and STL extensively in a project, and this is leading to over-long, error-prone types like shared_ptr< vector< shared_ptr<const Foo> > > (I'm an ObjC programmer ...
52 votes
2 answers
61k views

How to define a typedef struct containing pointers to itself?

I am writing a LinkedList in C, the below code represent my Node definition. typedef struct { int value; struct Node* next; struct Node* prev; } Node; I understand (or think that I do) ...
Kenny Cason's user avatar
  • 12.2k
52 votes
5 answers
13k views

Repeated typedefs - invalid in C but valid in C++?

I would like a standard reference why the following code triggers a compliance warning in C (tested with gcc -pedantic; "typedef redefinition"), but is fine in C++ (g++ -pedantic): typedef struct Foo ...
Kerrek SB's user avatar
  • 470k
52 votes
2 answers
1k views

What does "int typedef" mean?

I came across a new use of the keyword typedef in C++. What does this typedef declaration mean? int typedef foo;
vivek's user avatar
  • 5,121
51 votes
4 answers
205k views

Overloading operators in typedef structs (c++)

I want to make a typedef struct called pos (from position) that stores coordinates x and y. I am trying to overload some operators for this struct, but it does not compile. typedef struct { int x;...
tuket's user avatar
  • 3,510

1
2 3 4 5
63