answersLogoWhite

0


Best Answer

Consider the following structure:

  • struct data_t { /* ... */ };

The name of this type is struct data_t. This means that we must include the struct keyword whenever we declare any instances of the type or declare a pointer to the type:

  • struct data_t object;
  • struct data_t* ptr;

To remove the verbosity of the otherwise redundant structkeyword, we can use an alias:

  • typedef struct data_t data;
  • data object;
  • data* ptr;

To simplify things further, we can combine the alias with the structure's definition:

  • typedef struct data_t { /* ... */ } data;

Note that the _t suffix is conventionally used to denote a user-defined type as opposed to an alias. However, its usage is not consistent. For instance, the wchar_t type is not a type, it is implemented as an alias in the C standard library header:

  • typedef unsigned short wchar_t;

In C++, the typedef keyword is not required; it is retained purely for backward compatibility with C. Aliases are introduced with the using keyword, never with typedef. We only use typedef when we are explicitly writing code intended for compilation under both C and C++.

Note also that wchar_t (amongst others) is a built-in type in C++, so we don't need to include the C standard library to make use of it, unless we are writing code for C.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between structure and structure using typedef with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between Typedef and Reference in C plus plus?

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.


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.


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; };


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;


Are structures and pointers related in c plus plus?

Not really, but you can have: - a pointer pointing to a structure (FILE * is an example) - a pointer pointing to a structure-member (eg: struct tm tm; int *ip= &tm.tm_year) - a structure-member that is a pointer (any type) Example: typedef struct TreeNode { struct TreeNode *left, *right; int data; } TreeNode; TreeNode *root = (TreeNode *)calloc (sizeof (TreeNode), 1);

Related questions

What is the difference between Typedef and Reference in C plus plus?

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.


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.


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.


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; };


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... };


Are structures and pointers related in c plus plus?

Not really, but you can have: - a pointer pointing to a structure (FILE * is an example) - a pointer pointing to a structure-member (eg: struct tm tm; int *ip= &tm.tm_year) - a structure-member that is a pointer (any type) Example: typedef struct TreeNode { struct TreeNode *left, *right; int data; } TreeNode; TreeNode *root = (TreeNode *)calloc (sizeof (TreeNode), 1);


What is the difference between Typecast and Typedef?

Typecasting is to make a variable of one type, act like another type for one single operation. Type-def is to assign alternative names to existing types.


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;


What is the difference between enumrerated datatype and typedef?

Its very Simple that using Enumerated data type you are making special integers that flow within range, While a typedef is redefining data type with new name. Example: like defining enum Days{sun,mon,tue.....} makes an integer definition that can have 0-7 values So if u do following: Days x=sun; or Days x=0; then x=x+2; is 2 or tue and x=x+7; is 0 or sun again... Means its modulo 7 data type ................. While doing this: typedef Days WeekDays; renames Days as WeekDays Similary typedef int NUMBER; renames int as NUMBER .But wait it is one more name for the data type.. So simply enum creates a numeral modular datatype with a range while typedef creates another name for it. Rupesh K Joshi