tags:

views:

119

answers:

7
+2  Q: 

typedef,#define

Can anybody explain the difference between

#define int* char

and

typedef int* char;
+1  A: 

defines are handled by a preprocessor (a program run before the actual c compiler) which works like replace all in you editor.

Typedef is handled by the c compiler itself, and is an actual definition of a new type.

Forlan07
A: 

[typdef] allows you to introduce synonyms for types which could have been declared some other way. The new name becomes equivalent to the type that you wanted, as this example shows.

define is a c preprocessor macro, which will just replace occurences in your code.

The particular example your are showing seems a little weird to me, though. Maybe you can add some background information to your question.

The MYYN
+1  A: 

#define is a preprocessor directive. Before compiling, the text int* will be changed to char. It's something you typically don't do.

Typedef is a language instruction. It defines a new type, as Forlan07 just said.

Your example is very bad in either form. Fortunately though, it's illegal, as GMan pointed out.

Daniel Daranas
+2  A: 

They are different. Check this SO question.

#define is replacement which is handled during preprocessing

typedef is used to define a new type which is ignored during preprocessing

Praveen S
+5  A: 

There's no difference because both are illegal.

int* isn't a valid identifier for a macro, and even if you put a space in, neither is int, because it's a keyword and reserved. Even if you switched it to your likely intended #define char int*, it fails for the same reason.

And you cannot redefine the type char, so the typedef is a bust.


The difference, had you given examples that were legal, is that #define is a preprocessing directive. It only does textual replacement, before the compiling stage. Contrarily typedef defines a new type, and that new type identifier respects "the rules" just like any other type.

GMan
A: 

typedef assigns alternative names to existing types. e.g: typedef unsigned long size_t

Now in code you can replace unsigned long with size_t unsigned long temp; is the same as size_t temp;

'#define' is a preprocessor directive. It causes the compiler to go through the source code, replacing every occurrence of macro-name with replacement-string e.g. #define PI 3.14159

The examples that you have chosen will give compilation error.

rohitude
A: 

A type alias defined using a macro rather than a typedef has the flexibility to be undefined or redefined whereas a typedef does not. This is why you may see type aliases defined this way. It is both a weakness and an advantage. The number of C libraries that define their own types with common names like UINT, DWORD or BOOL for example is countless, and the cause of many problems when using multiple third-party libraries. At least with a macro you can force them to be undefined, or check for existing definitions; if a typedef is used, you may have a symbol clash problem than makes it difficult to integrate such code.

In C++ the problem is better overcome by encapsulating the third-party code in a namespace, then the typedef would remain the better solution to type aliases.

Clifford