Typedef
Typedef
STEEVIN
Roll No :41
Introduction
12/08/21 2
Basic information
• typedef is a keyword in the C
• Its used to create new data types
• Once the user defined datatype is created the
variables can be declared in terms of the new
data type.
• The general syntax is
typedef type new-type;
12/08/21 3
Example:
typedef int km_per_hour ;
typedef int points ;
km_per_hour current_speed ;
points high_score ;
...
12/08/21 4
typedef int aaa, bbb, ccc;
typedef int ar[15], arr[9][6];
typedef char c, *cp, carr[100];
/* now declare some objects */
/* all ints */
aaa int1;
bbb int2;
ccc int3;
ar yyy; /* array of 15 ints */
arr xxx; /* 9*6 array of int */
c ch; /* a char */
cp pnt; /* pointer to char */
carr chry; /* array of 100 char */
The general rule with the use of typedef is to
write out a declaration as if you were declaring
variables of the types that you want.
12/08/21 6
The statement:
typedef long int FOUR_BYTE_INT;
makes the name FOUR_BYTE_INT synonymous with long int. The
following two declarations are now identical:
long int j;
FOUR_BYTE_INT j;
typedef struct {
char month[4];
int day;
int year;
} BIRTHDAY;
typedef char A_LINE[80]; /* A_LINE is an array of
* 80 characters */
12/08/21 7
Simplifying Complex Declarations
12/08/21 8
Using typedefs for Arrays
The following two examples illustrate what can happen when you mix
pointers and typedefs that represent arrays. The problem with the first
program is that ptr points to an array of 80 chars, rather
than a single element of a char array. Because of scaling in pointer
arithmetic, the increment operator adds 80 bytes, not one byte, to ptr.
wrong
typedef char STR[80];
STR string, *ptr;
main()
{
ptr = string;
printf("ptr = %d\n", ptr);
ptr++;
printf("ptr = %d\n", ptr);
}
*** Run-Time Results ***
ptr = 3997696
ptr12/08/21
= 3997776 9
Right code
12/08/21 10
Example program
• /************************************************************************
• * * Purpose: To demonstrate 'linked lists' This program will build a
• * linked list and place data into it. When the data is exausted
• * the contents of the list are O/P.
• * This example shows the use of 'typedef' on linked lists.
• *
• * This is a "First in First out" (FIFO) list.
• * * ************************************************************************/
• #include <stdlib.h> /* malloc */
• /************************************************************************/
12/08/21 11
/************************************************************************/
main()
{
linklist *start_pointer; /* Define pointers to the structure */
linklist *next_pointer;
12/08/21 12
/*===================================================================*
=
=
= Build a LINKED LIST and place data into it. =
=
=
*===================================================================*/
strcpy(next_pointer->name, names[count]);
next_pointer->age = ages[count];
12/08/21 13
/* Loop until all data has been read */
while ( ages[++count] != 0 )
{
/* Reserve more memory and point to it */
next_pointer=next_pointer->next_rec;
strcpy(next_pointer->name, names[count]);
next_pointer->age = ages[count];
}
next_pointer->next_rec=NULL;
/*===================================================================*
=
=
= Traverse the linked list and O/P all the data within it. =
=
=
*===================================================================*/
12/08/21 14
next_pointer=start_pointer;
/************************************************************************
*
* Program results.
*
* Martin 32
* John 43
* Alex 29
*
************************************************************************/
12/08/21 15
THANKYOU
THANKYOU…….
…….
12/08/21 16