Searching for "typedef"

Q:

Improve the following code using typedef.

struct node

{

      int data1;  float data2;

       struct node *left;

       struct node *right;

};

struct node *ptr;

ptr = (struct node *) malloc (sizeof (struct node) ); 

Answer

typedef struct node * treeptr


typedef struct node


{


         int data1;


         float data2;


         treeptr *left;


         treeptr *right;


}treenode;


treeptr ptr;


ptr = ( treeptr ) malloc ( sizeof ( treenode ) );

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

Q:

In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No>

typedef struct employee *ptr;

struct employee

{

       char name[20];

        int age;

        ptr next;

};

Answer

Yes

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

Q:

Point out the error, ifany, in the followingb code?

typedef struct

{

     int data;

     NODEPTR link;

} *NODEPTR;

 

Answer

A typedef defines a new name for a type, and in simpler cases like the one shown below you can define a new structure type and a typedef for it at the same time.


typedef struct


{


    char name[20];


    int age;


} emp;


However, in the structure defined in this question, there is an error because a typedef declaration cannot be used until it is defined. In the given code fragment the typedef declaration is not yet defined at he point where the link field is declared.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming