1. Typedef:-
- The typedef is a C facility to create new data types. It does it by giving a user defined name to existing types.
- For example following declarations
typedef int counter;
- creates anew data type counter which is nothing but integer value. Above declaration makes counter a synonym of integer. Once this declaration is made, counter is a data type which can be used to create variables just like other types and can also be used in type casting. The following declaration
counter c1, c2;
- creates two variables c1 and c2 of counter type. Remember that c1 and c2 can possess integer values as counter is synonym of integer. Following examples illustrate thier use
c1++;
c2=(counter)5.8;
printf(“%d”,c2+3);
- The typedef is very useful in case of structure to give them alternative name so that every time we need not write word struct followed by tag_name in order to create structure variables. Rather we can use this alternative name. Follwing example illustrates the concept.
typedef struct student
{
int rno;
char name[10];
int age;
}ST;
- Now ST is name given to structure type student. To create structure variable s, we can use just
ST s;
instead of
struct student s;
That means ST is synonym for struct student.
2. Accessing Structure Members:-
- Members of a structure can be accessed and used individually in a program. Following syntax is used to access these members of a structure.
structure-variable.structure-membername
- structure-variable is the name of structure variable and structure-membername is the name of member of structure. Structure member operator “.” (dot operator) connects member name to structure variable name. For example,
s1.age
denote value of member age of a structure variable s1 which is of student type. The following statement assigns 21 to s1.age
s1.age=21;
The roll number of student s1 can be read from keyboard as
scanf(“%d”,&s1.rno);
- Following examples illustrates use of structure members in different statements.
s1.rno=15;
(s2.rno)++;
printf(“%d\n”,s2.rno);
strcpy(s1.name,”aarzoo”);
printf(“%s\n”,s1.name);
scanf(“%d%s”,&s2.rno,s2.name);
