Beruflich Dokumente
Kultur Dokumente
Structure
Structure is a way in which a complex userdefined data type can be created using simple
types.
For example, an employee is a data-type
consisting of employee number, name and
salary.
Structure allows us to define employee, and then
assign variables to the employee structure type.
Syntax
Defining structure:
struct struct-name{
data-type variable-name1;
data-type variable-name2;
members
data-type variable-namen;
};
Declaring variable of struct-type:
struct struct-name variable-name;
Example
struct employee{
int emp_num;
defining
char name[30];
double salary;
};
struct employee emp1,emp2;
declaring
Accessing members
Structure members can be accessed using . operator
on the structure variable.
Example:
emp1.emp_num, emp2.name
Structure members can be initialized during the
declaration as:
struct employee
emp={123,Bobby,345.50};
Example- simple
emp1.c
struct emp{
int num;
char name[30];
double sal;
};
struct emp emps;
main(){
read();
display();
}
read(){
printf("enter emp number:");
scanf("%d",&emps.num);
printf("enter name:");
scanf("%s",emps.name);
printf("enter salary:");
scanf("%lf", &emps.sal);
}
display(){
printf("Displaying data \n");
printf(" emp number: %d\n", emps.num);
printf(" emp name: %s\n",emps.name);
printf(" emp salary: %5.2lf\n",emps.sal);
}
Output:
enter emp number:123
enter name:Kanaka
enter salary:12340
Displaying data
emp number: 123
emp name: Kanaka
emp salary: 12340.00
ball.c
Structure Pointers
A pointer variable can be declared that points to a structure
like it points to an ordinary variable.
Example:
struct emp emps={123,"Ganga", 12000};
struct emp *e=&emps;
To access members through structure pointer, a special
kind of symbol is used.
Example:
enum, ename
Structure pointers are required when we pass a structure
variable to a function and we want the changes made in the
variable visible in the calling function.
struct emp{
emp2.c
int num;
char name[30];
double sal;
};
main(){
struct emp emps={123,"Ganga", 12000};
increment(&emps);
emps.sal
emps.num
display(&emps);
123
12000
Ganga
}
2001
emps.name
2001
13200
Question?
Nested structure
A structure can have as its member a structure
variable.
Example:
struct name{
char fname[10];
char lname[10];
};
struct student{
struct name nm;
int rollno;
Note: another way of
} stud;
main(){
printf("enter first and last name: ");
scanf("%s%s",
stud.nm.fname,stud.nm.lname);
printf("\nenter roll number:");
scanf("%d", &stud.rollno);
printf("displaying data\n");
printf("Name:%s %s\n",
stud.nm.fname,stud.nm.lname);
printf("roll no %d\n", stud.rollno);}
Output:
enter first and last name: John Ray
enter roll number:12345
displaying data
Name:John Ray
roll no 12345
Bit Fields
The smallest datatype as we know is char.
But in structure it is possible to declare a smaller
object called bit field.
Bit fields are used when efficient use of memory
is a serious concern.
Example:
struct A {
All of it can be accommodated
int a:3;
in a single 12 bits
int b:7;
Note that compiler allocates at
int c:2;
least a chars worth memory even
};
if one bit needs to be stored.
Non-portability issue:
int x:18;
On system where int is 16 bits long, this is illegal!
Unions
Union allows variables of different data types to share
the same memory space. For example, a variable of
int type and a variable of type char share same
memory space. That means this memory space can be
treated either as int or as char.
Union allows us to choose between types of data
based on the requirement of application.
Syntax
Defining union:
union union-name{
data-type member-name1;
data-type member-name2;
data-type variable-name;
};
Declaring variable of union-type:
union union-name variable-name;
Accessing members:
variable-name.member_name
struct X{
short a;
char c[2];
};
union X{
short a;
char c[2];
};
a
1000
1002
c[0]
c[1]
1003
a
1000
c[0] 1001
c[1] 1002
1004
un.c
main(){
union X{
Output:
16449
A
@
short int a;
char c[2];} x1;
x1.a=16449;
Output depends on
the endianess of the
system you are using
printf("%d\n",x1.a);
printf("%c\n",x1.c[0]);
printf("%c\n",x1.c[1]);
c[0]=ascii(65)=A
c[1]=ascii(64)=@ a=16449
0 1 0 0 0 0 0 0 0
Little endian
0 1 0 0 0 0 0
Big endian
0 1
Variant records
A single structure that can hold different values.
Example:
struct Student{
char name[50];
int regno;
unsigned char indian:1;
union{
char electionIdNumber[20];
char passportnumber[20];
}identity;
};
Enumerated data-type
Enumeration allows us to define a data-type for which a
predefined set of values are defined. Any variable of this
data-type then can have one of the predefined values.
For example, gender can have only one of the two values
male or female. Anything else would give error.
enum gender{
male,
enum constants
female
};
main(){
enum gender g1,g2;
g1=male;
Assigning any other value would be an error.
g2=female;
print(g1);
print(g2);
Output:
}
Male
print(enum gender g){
Female
if(g==male)
printf("Male \n");
else
printf("Female");
}
Internal working
Internally compiler treats enums as integers.
Hence the values that enum constants take start
from 0.
In the previous example male is internally stored
as 0 and female is 1.
These values can be changed by explicitly :
enum gender{
male=10,
female=20
};
typedef
typedef is used to give a different name to a data type.
Let us understand this with an example:
struct fullname{
char fname[10];
char lname[10];
};
To declare variable of the above type
struct fullname n1,n2;
If we have many such declarations throughout the code then typing this
could be tedious.
Using typedef, we could just type
name n1,n2;
enum gender{
male,
female
};
typedef enum gender GEN;
struct emp{
int num;
char name[30];
double sal;
GEN gen;
};
typedef struct emp EMP;
main(){
EMP emps={123,"Ganga", 12000, female};
increment(&emps);
display(&emps);
}
increment(EMP *e){
e->sal=e->sal*0.10+e->sal;
}
display(EMP *e){
printf("Displaying data \n");
printf(" emp number: %d\n",e->num);
printf(" emp name: %s\n",e->name);
printf(" emp salary: %5.2lf\n",e->sal);
if(e->gen==male)
printf(" Gender: Male");
else
printf(" Gender: Female");
}