Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

typedef

STEEVIN
Roll No :41
Introduction

Typedef is the definition maker


which allows the user to create
new data types that are equivalent
to the existing data type.

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

void congratulate(points your_score) {


if (your_score > 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.

Where a declaration would have introduced


names with particular types, prefixing the whole
thing with typedef means that, instead of getting
variables declared, you declare new type names
instead.

Those new type names can then be used as the


prefix to the declaration of variables of the new
type.
.

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;

Abstract Global Types


Typedefs are useful for abstracting global types that can be used
throughout a program, as shown in the following structure and array
declaration:

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

You can use typedefs to simplify complex declarations.


For example:

typedef float *PTRF, ARRAYF[], FUNCF();

This declares three new types called PTRF (a pointer to a float),


ARRAYF (an array of floats), and FUNCF (a function returning a
float).

These typedefs could then be used in declarations such as the


following:
PTRF x[5]; /* a 5-element array of pointers to floats */
FUNCF z; /* A function returning a float */

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

typedef char STR[80];


STR string;
char *ptr;
main()
{
ptr = string;
printf("ptr = %d\n", ptr);
ptr++;
printf("ptr = %d\n", ptr);
}
*** Run-Time Results ***
ptr = 3997696
ptr = 3997697

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 */

• /************************************************************************/

• /* Declare a structure and give it a


• * data type name with 'typdef' */
• typedef struct x {
• char name[20];
• int age;
• struct x *next_rec;
• } linklist;

12/08/21 11
/************************************************************************/

main()
{
linklist *start_pointer; /* Define pointers to the structure */
linklist *next_pointer;

/* Create some data to be placed in the


* Linked list.
*/
char *names[]=
{
"Martin",
"John ",
"Alex ",
""
};

int ages[]={32, 43, 29, 0};

int count=0; /* General purpose counter. */

12/08/21 12
/*===================================================================*
=
=
= Build a LINKED LIST and place data into it. =
=
=

*===================================================================*/

/* Initalise 'start_pointer' by reserving


* memory and pointing to it
*/

start_pointer=(linklist *) malloc (sizeof (linklist));

/* Initalise 'next_pointer' to point


* to the same location.
*/
next_pointer=start_pointer;

/* Put some data into the reserved


* memory.
*/

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_rec=(linklist *) malloc (sizeof (linklist));

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;

while (next_pointer != NULL)


{
printf("%s ", next_pointer->name);
printf("%d \n", next_pointer->age);
next_pointer=next_pointer->next_rec;
}
}

/************************************************************************
*
* Program results.
*
* Martin 32
* John 43
* Alex 29
*
************************************************************************/

12/08/21 15
THANKYOU
THANKYOU…….
…….

12/08/21 16

You might also like