Friday, 24 July 2015

TYPEDEF CONCEPT

The typedef declaration provides a way to create an alias that can be used anywhere in place of a (possibly complex) type name.


The typedef-names are aliases for existing types, and are not declarations of new types. Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again. Typedef names are only in effect in the scope where they are visible: different functions or class declarations may define identically-named types with different meaning

ALL DATA TYPES CAN HAVE TYPEDEF DECLARATION 
EX INT ,CHAR LONG INT , LONG LONG INT ..

Q-- WHY static VARIABLE CAN'T BE DECLARED TYPEDEF .
ANS-
typedef doesn't declare an instance of a variable, it declares a type (type alias actually).
static is a qualifier you apply to an instance, not a type.

OR
ypedef doesn't declare an instance of a variable, it declares a type (type alias actually),
static is a qualifier you apply to an instance, not a type, so you can use static when you use the type, but not when you define the type. Like this..
typedef int int32;
static int32 foo

NOTE-this will also  not work #define SI static int.

Q- what is the difference between typedef and #define 
1>The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, like you can define 1 as ONE etc.

2>The typedef interpretation is performed by the compiler where as #define statements are processed by the pre-processor.

3>#define will just copy-paste the definition values at the point of use, while typedef is actual definition of a new type.

4>typedef follows the scope rule which mean if a new type is defined in a scope(inside a function), then the new type name will only be visible till the scope is there.


note -
with the help of typedef we can declare pointer of any data type also 
ex - typedef  int * ptr;
 int  t;
 ptr=&t;

note -- any data type can be typedef at any where in the program .
ex-

No comments:

Post a Comment