answersLogoWhite

0


Best Answer

You use typedef to declare a synonym for an existing type. It's generally just a way of reducing a complex or cumbersome declaration outside your code to a more simplified, more easily understood declaration that you can use inside your code.

Cumbersome example:

void DoStuff( void (*)(int&, char& ), int&, char&); // Huh? Do what?

Simplified example:

typedef void (*pFunc) ( int&, char& );

void DoStuff( pFunc, int&, char& ); // Aha! It's a function pointer!

In relation to C structures, typedef provides a way to declare and name user-defined types, primarily so you don't have to use the struct keyword in the variable declaration. C++ structures are more flexible and the typedef keyword is optional.

Structure examples for C:

struct hard

{ int i;double f;

};

// typedef is optional in C++, but required in C.

typedef struct

{

int i;

double f;

} easy;

int main()

{

struct hard hs; // Requires struct keyword

easy es; // Same as C++.

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Example:

typedef struct MYTYPE {

...

...fields...

...

... struct MYTYPE *self_pointer_if_needed;

...

} MYTYPE;

MYTYPE myvar, *myptr;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How typedef feature used with structure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a c program that define a structure linesegment that will represent a line segment by the co-ordinate of its endpoint?

There will a part like this: typedef struct Point { double x, y; } Point; typedef struct LineSegment { Point from, to; } LineSegment;


What is the keyword used in combination with a valid c data type to redefine the name of the data type?

The keyword is typedef. typedef int myInteger; myInteger is now an alias for int. This also works for more complex data types, such as arrays and structures. typedef struct { int a; double b; char c; } _myStruct myStruct; _myStruct is now an alias for the structure that contains a, b, and c. I also instantiated one of them, and called it myStruct. You could now repeat this with... _myStruct xyz; ...which would instantiate another one, called xyz. You can reference the members as xyz.a, xyz.b, and xyz.c.


Can a Structure contain a Pointer to itself?

Yes, it is quite common. Example: struct List { struct List *Next; int value; } typedef struct List List; Example2: typedef struct Tree Tree; struct Tree { Tree *left,*right; int value; };


What is typedef declaration?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.


What is the difference between a macro and typedef?

A Macro is a preprocessor directive means that before compilation the macros are replaced. Where as typedef is defining a new data type which is same as the existing data type. Syntax: typedef Existing datatype New datatype For example typedef int NUMBER; Here NUMBER (New datatype)is defined as a data type which contains the properties same as int(Existing datatype). You can declare a variable of int as NUMBER a; is same as int a; similarly typedef int* NUMBERPOINTER; NUMBERPOINTER a; Here a is a pointer of integer type.

Related questions

What is the use of type def in c?

A typedef is used to define a type. The clue is in the name: typedef = type definition.


What is the most important feature used to classify animals?

The most important feature used to classify animals is Body structure!


What is the main use for type def seems to be defining structures?

When you define a structure, C does not provide a type for that structure. In order to subsequently declare an instance of that structure, you need to use the word struct again. The typedef allows you to declare a type equivalent to the structure. For example... struct person { char* name, int phone}; struct person myperson; With typedef, you can simplify to... typedef struct person { char * name, int phone} person;person myperson; In C++, this is automatic, but not in C.


What word feature may be used as an underlining structure to an agenda?

pitch


How do you write a c program that define a structure linesegment that will represent a line segment by the co-ordinate of its endpoint?

There will a part like this: typedef struct Point { double x, y; } Point; typedef struct LineSegment { Point from, to; } LineSegment;


What is meant by array with in structure in c language?

It means a structure has a member that is an array: typedef struct foo { int x[42]; // an array of 42 integers // other members... };


What term could be used to describe an Egyptian pyramid?

Majestic ancient structure.


Is possible define a structure with extern keyword?

extern is used only when there is a variable or a function name. so here's what you can do, typedef struct{ int data; }my_struct; extern my_struct my_new_struct; Compilers takes this as a *type*.


Typedef int a a a Is this possible in C?

No, but 'typedef int a;' is possible, it defines the type 'a'.


What are the Uses of typedef?

the purpose of typedef is to redefine the name of an existing variable type. e.g. typedef unsigned long int T; T v1,v2;


Write data structure for creation operation on singly linked list?

typedef struct ListElement {struct ListElement *next;long data;} ListElement;


How can one create a pointer to structure in C?

Create a pointer of the type (pointer to struct) and assign the address of an instance of the structure to your pointer: typedef struct x { /* ... */ }; struct x my_structure; struct x* ptr = &my_structure;