Status
Not open for further replies.

QwertyManiac

Commander in Chief
type definition is used to declare variables and store them in a type of special variable as a special array...

Found this for sureness :
Although typedef is thought of as being a storage class, it isn't really. It 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.

typedef int aaa, bbb, ccc;
typedef int ar[15], arr[9][6];
typedef char c, *cp, carr[100];

/* now declare some objects */

/* all ints */
aaa int1;
bbb int2;
ccc int3;

ar yyy; /* array of 15 ints */
arr xxx; /* 9*6 array of int */

c ch; /* a char */
cp pnt; /* pointer to char */
carr chry; /* array of 100 char */

The general rule with the use of typedef is to write out a declaration as if you were declaring variables of the types that you want. Where a declaration would have introduced names with particular types, prefixing the whole thing with typedef means that, instead of getting variables declared, you declare new type names instead. Those new type names can then be used as the prefix to the declaration of variables of the new type.

The use of typedef isn't a particularly common sight in most programs; it's typically found only in header files and is rarely the province of day-to-day coding.

It is sometimes found in applications requiring very high portability: there, new types will be defined for the basic variables of the program and appropriate typedefs used to tailor the program to the target machine. This can lead to code which C programmers from other environments will find difficult to interpret if it's used to excess. The flavour of it is shown below:

/* file 'mytype.h' */
typedef short SMALLINT /* range *******30000 */
typedef int BIGINT /* range ******* 2E9 */

/* program */
#include "mytype.h"

SMALLINT i;
BIGINT loop_count;

On some machines, the range of an int would not be adequate for a BIGINT which would have to be re- typedef'd to be long.

To re-use a name already declared as a typedef, its declaration must include at least one type specifier, which removes any ambiguity:

typedef int new_thing;
func(new_thing x){
float new_thing;
new_thing = x;
}

As a word of warning, typedef can only be used to declare the type of return value from a function, not the overall type of the function. The overall type includes information about the function's parameters as well as the type of its return value.

/*
* Using typedef, declare 'func' to have type
* 'function taking two int arguments, returning int'
*/
typedef int func(int, int);

/* ERROR */
func func_name{ /*....*/ }

/* Correct. Returns pointer to a type 'func' */
func *func_name(){ /*....*/ }

/*
* Correct if functions could return functions,
* but C can't.
*/
func func_name(){ /*....*/ }

If a typedef of a particular identifier is in scope, that identifer may not be used as the formal parameter of a function. This is because something like the following declaration causes a problem:

typedef int i1_t, i2_t, i3_t, i4_t;

int f(i1_t, i2_t, i3_t, i4_t)/*THIS IS POINT 'X'*/

A compiler reading the function declaration reaches point ‘X’ and still doesn't know whether it is looking at a function declaration, essentially similar to

int f(int, int, int, int) /* prototype */

or

int f(a, b, c, d) /* not a prototype */

—the problem is only resolvable (in the worst case) by looking at what follows point ‘X’; if it is a semicolon, then that was a declaration, if it is a { then that was a definition. The rule forbidding typedef names to be formal parameters means that a compiler can always tell whether it is processing a declaration or a definition by looking at the first identifier following the function name.

The use of typedef is also valuable when you want to declare things whose declaration syntax is painfully impenetrable, like ‘array of ten pointers to array of five integers’, which tends to cause panic even amongst the hardy. Hiding it in a typedef means you only have to read it once and can also help to break it up into manageable pieces:

typedef int (*a10ptoa5i[10])[5];
/* or */
typedef int a5i[5];
typedef a5i *atenptoa5i[10];

Try it out!

Sourcee :
*publications.gbdirect.co.uk/c_book/chapter8/typedef.html
 

Thor

Ambassador of Buzz
Hello There !!!
Every variable has a data type. typedef is used to define new data type names to make a program more readable to the programmer.

Consider This code:

#include<stdio.h>
#include<conio.h>

void main()
{
int marks;
marks=100;
}

Now Consider This :

#include<stdio.h>
#include<conio.h>

void main()
{
typedefine int student;
student marks;
marks=100;
}

---------------------------
Both the codes Does the same thing - > Creating an INT datatype "marks" and giving it a value of 100. The 2nd Program does it in a more stylish way. How ? ->
Code:
typedefine int student;
here typedefine is a keyword which essentially defining "student" as a new from of datatype which is identical to "int" .
This makes a program much more readable. Here in this code what is "marks" ? Of course marks of a student...so Defining marks as a variable of student datatype make a kind of relation..doesn't it....

If u know Structures...
Code:
struct var
{
int data1;
int data2;
char data3;
};

Here u hv created a USER DEFINED Data type "var".
Thus to create a variable of the type "var" ->
Code:
struct var a;
code is needed thus creating a Variable a of "var" datatype.

Had I written :
Code:
struct var
{
int data1;
int data2;
char data3;
};
typedef struct var newtype;
newtype a;

What Happens ?? newtype has been defined as a new user defined data type -> struct var.. What is its use ??
While Passing a Structure to a function We Write :
Code:
void SomeFunc(struc var a)
{
---
---
}
now everytime u define a function which takes Structure variables as Arguments u will hv to definer the Function in the abv format.
But had we used:
Code:
typedef struct var newtype;

we cud hv written :
Code:
void SomeFunc(newtype a)

Isn't this much more Comforting ? To see similar way the Functions passing Arguments to themselves...instead of adding the "Damned"
keyword "Struct" to it everytime..... I hope I was able to clear..it.
4 more vistit This
================

I will tell u one Thing.
If u r serious abt Learning C and then C++ , Get :
"Complete Reference to C++" by Herbert Schildt , TMH Publication... Best Seller at Amazon.com [ !! ] .
 

Thor

Ambassador of Buzz
Yeppp.. Similar Link ..True... Becoz We did the same thing.... "Typedef" at Google... and the 4th link...points to that... also I didn't know u were posting it when I was busy writing my thing...

Anyways I'm editing that link...

C++ is after C. And that is Why Mr.Herbert Schildt has dealt with all possible C syntaxes and its uses 1st and then parallely he has introduced the differneces in C++. But in part II of that Same book... He has started full fledged discussioin in C++.

An Eg:
#include<stdio.h>
#include<conio.h>

struct example
{
int a;
int b;
};

void display(example);
void main()
{
example var1;
var1.a=2;
var2.b=3;
display(var1);
}

void display( example var1)
{
printf(" a = %d ",var1.a);
printf(" b = %d ",var1.b);
getch();
}
This abv code shall run Fine in C++ but will throw up error in C.
Why ?
Becoz C needs to be told that example is a userdefined Data Type Thru.. struct examlpe ...

Thus if the line ->
example var1;
is replaced with
struct example var1;
and
void display(example var1)
is replaced with
void display(struct example var1)

then In C the code shall run Fine....
He has not forgotten to mention all this.....
It is an excellent book 4 many more reasons. I think all C/C++ programmers shud give this one a try.
 
Status
Not open for further replies.
Top Bottom