Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how typedef is usued??

Status
Not open for further replies.

ssrc

Programmer
May 20, 2003
5
IN

hi
i want to know about how typedef is usued?i found that it is for user defined data type.

ex:
typedef struct
{
int ac_no;
char ac_type;
char name[80];
flaot balance;
} record;

record oldcustomer,newcustomer;

...but same thing we can write as below..

struct record
{ int ac_no;
char name[80];
floa balance;
};
struct record oldcustomer,newcustomer;

-----this has the same effect as earlier. i think latter one is more readable. but still i found in book that "....typedef feature is convenient when defining structure because it eleminates repetation of structure tag" ( schaum's series c programming) how????????

i think latter one is good. is there any advantage of using typedef over structure??

or can u say in any other place where this typedef is usefull?
thanks
 
Code:
void myfunc( struct record* prec);
vs.
Code:
void myfunc( record* prec);

Many people find the second more readable, if you do not then Ok do it your way i guess. ;-)

-pete
 
Whether to use typedef or not is mainly a style issue.

However, many people would agree that typedef'ing function pointers is a good idea because you save typing *and* you make a complicated declaration look simpler.

void **(*)(const void *, int, size_t, long)
get_func(const char *s, int)
{
/* Returns pointer to function returning double
* pointer to void, accepting pointer to const
* void, int, size_t and long. */
}

Or

typedef void **(*foo_f)(const void *, int, size_t, long)
foo_f get_func(const char *s, int) { /* ... */ }

In some cases you want to hide the real type from the user. Say you have a stack module:

stack.h
-------
/* Type to represent # of items currently in stack. */
typedef unsigned long stack_count_t;

/* Returns # of items currently in stack. */
stack_count_t
stack_count(const struct stack *);

/* ... */

Say a large number of developers download and use your stack module in their programs. If you didn't use a typedef like above, their programs would be littered with stuff like this:

unsigned long count = stack_count(stack);

If some time down the road you wanted to change the data type from unsigned long to something else (say, unsigned long long) and maybe make some bug fixes in the process, your user base would have to change all of those unsigned long declarations to the new type if they used your new module.

With the typedef, you avoid this issue because their code is ignorant of what exact data type is used.
 
When you say "using typedef over structure," it's not really a valid question. The two are incomparable. A structure is an aggregate data type. A typedef is an alias for a type.


The reason most people tend to find

Code:
record r1;

more readable than

Code:
struct record r1;

is that it more closely mirrors the declaration

Code:
int x;


There's really no reason you should have to communicate that a given type is a struct and not a primitive type or enum or typedef or something else. You should just be able to refer to it by name. In fact, doing otherwise is somewhat confusing.

In C++ and in C99 (as well as in Java and C# and pretty much any modern language that uses something comparable to "structs"), there is no need to use the struct tag. You just use the type's name.

Lots of C code contains typedefs like that one to emulate that "type name only" capability before it really existed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top