Tuesday, May 8, 2012

WHAT IS THE USE OF typedef in C

what is the use of typedef in C?

struct _test{
unsigned int count;
unsigned char name[67];

}access_test;
Now you can access this structure using access_test.
If you want to create another copy of this structure then you
have to go to the structure decleared  place and add extra
accessing variable 
struct _test{
unsigned int count;
unsigned char name[67];

}access_test0,access_test1;

Now i want create a library file that is only usable .Not readable
(like some library file )
then how will you add one more copy of that structure in that place
typedef will give a easy solution for this

typedef struct _test{
unsigned int count;
unsigned char name[67];

}access_test;

Above will be hidden in library file, but  with the help of
access_test you can create your own copy of structure

access_test access_test1;


while using macro or typedef using capital(Upper case) letter is good practice

typedef struct _test{
unsigned int count;
unsigned char name[67];

}ACCESS_TEST;

ACCESS_TEST access_test1;


/*
 * File:   main.c
 * Author: praveen fedrick
 *
 * Created on May 8, 2012, 10:27 PM
 */

#include <stdio.h>
#include <stdlib.h>



typedef struct _test{
unsigned int count;
unsigned char name[67];

}ACCESS_TEST;

ACCESS_TEST access_test1;



int main()
{
   
access_test1.count=10;
sprintf(access_test1.name,"praveen fedrick");
printf("Name %s and his count is %d",access_test1.name,access_test1.count);

}


No comments:

Post a Comment