Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does typedef in C/C++ really create a NEW type by combining the compound type (e.g. int*)?

recently I found typedef in my code works really different than what I proposed. An example like this:

typedef int *ptype;
ptype v1 = 0, v2 = 0;

The result: both v1 and v2 were defined as a pointer to int type. But if you simply replace ptype by int * in the second sentence as int *v1 = 0, v2 = 0; or int* v1 = 0, v2 =0;, only v1 will be the pointer and v2 is normal int. It seems typedef does not do a simple replacement. What's more, when it comes to complicate modifier like:

typedef int *ptype;
const ptype v3 = 0;

The result will be: v3 is a const pointer, not a pointer to the const int if we write const int *v3 = 0;. In the above code const stands as the modifier to the whole ptype, not the int inside ptype. Thus it really looks like typedef combines the compound type int* and creates a new type.

However, the authoritative C/C++ reference website cplusplus says "typedef does not create different types. It only creates synonyms of existing types." so I was really confused and hope someone can help explain the behavior of typedef. Thanks!

like image 638
Fopopo Avatar asked Jan 15 '13 09:01

Fopopo


People also ask

Does typedef create a new type?

Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.

What is the purpose of typedef in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

What is typedef in C explain with example?

typedef is used to define new data type names to make a program more readable to the programmer. For example: | main() | main() { | { int money; | typedef int Pounds; money = 2; | Pounds money = 2 } | } These examples are EXACTLY the same to the compiler.

What is typedef declaration in C?

The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.


2 Answers

"Synonym" does not mean "text replacement". ptype is not literally expanded to int * by the preprocessor or anything.

What it means is that you can do things like:

typedef int *ptype;
ptype a;
int *b;

a = b;   // OK

The assignment is valid because ptype and int * are the same type; there is no type conversion or cast required.

typedef simply lets you give a new name to an existing type. But that name combines every aspect of the existing type into an indivisible entity, such that e.g. ptype a, b; is equivalent to ptype a; ptype b; (and const ptype means "const pointer-to-int" because ptype means "pointer-to-int").

In other words, the new names created by typedef behave like built-in keywords as far as declaring things goes, but the actual types represented by those names are the same.

like image 109
melpomene Avatar answered Sep 28 '22 02:09

melpomene


When cplusplus.com says that typedef doesn't create a new type, what they mean is that anywhere where you can use a value of ptype you can also use a value of type int* and vice-verse. So if you define a function that takes a ptype parameter, you can pass it an int* without any conversions being necessary.

That does not mean that a typedef is implemented as purely textual substitution like a #define would be.

like image 45
sepp2k Avatar answered Sep 28 '22 01:09

sepp2k