Notes
typedef
Content of the notes (2)
Page 1
Typedef
if We write program
#include <stdio.h>
int main(void)
{
struct node
{
int data;
struct node * next;
}node;
node *tmp;
tmp=( node *)malloc(sizeof(node));
scanf("%d",&tmp->data);
printf("%d",tmp->data);
// your code goes here
return 0;
}
it gives error
prog.c: In function ‘main’: prog.c:12:9: error: ‘tmp’ undeclared (first use in this function) node *tmp;
If I change declaration of tmp as
struct node * tmp;
then error gives
prog.c:14:14: error: expected expression before ‘)’ token tmp=( node *)malloc(sizeof(node));
Because typecasting is done with datatype and node * is not a datatype
So to make my own datatype we use typedef
and So a kind of program is and will remove both above two errors
#include <stdio.h>
int main(void)
{
typedef struct node
{
int data;
struct node * next;
}node;
node *tmp;
tmp=( node *)malloc(sizeof(node));
scanf("%d",&tmp->data);
printf("%d",tmp->data);
// your code goes here
return 0;
}
Page 2
Information
- Description
- Computer Science Note on typedef, created by Anirudh pandey on 10/04/2018.
- Created by
- Anirudh pandey · Apr 2018
- Updated
- Apr 10, 2018
- Views
- 0
- Tags
- Rating