Structure in C
Structure in C
Structure in C allows us to create a complex data type. This new type can
hold multiple values of different types. If you are familiar with enum in C,
the syntax to define a structure will be easy for you.
Declaring a Structure
structure element 1 ;
structure element 2 ;
structure element 3 ;
......
......
};
The keyword struct tells the compiler that we are creating a structure
The <structure name> is the name of the structure. We can use it later to
create variables. We call these variables instances.
Inside the body, we define the members. We can define as many as we need.
Their type could be any valid C type, including other structures, unions etc.
After the body we have the option to create a list of variables of the current
type. To do this, we just write their names and separate them with a comma.
struct Employee
char name[30];
char address[50];
int age;
};
Creating instances
There are several ways to create variables of the struct type that you
created. The first way is to list the variable names after the structure
definition like this:
struct Employee
char name[30];
char address[50];
int age;
I like to separate the types and the variables, so I prefer the second way.
After you define the structure, use its Tag and the keyword struct to create a
variable:
Accessing members
To access a member of a structure in C we use the dot ‘.’ and the arrow ‘->’
operators.
We use the dot operator when we work with an instance:
worker1.age = 20;
worker2->age = 20;
struct Employee
struct Address
char country[30];
char city[30];
char street[30];
char addressField[30];
} address;
char name[30];
int age;
};
or like this:
address.h
struct Address
{
char country[30];
char city[30];
char street[30];
char addressField[30];
};
employee.h
struct Employee
char name[30];
int age;
};
Padding
struct A
char symbol;
int number;
short id;
};
struct A
};
struct A
int number;
short id;
char symbol;
};
Now only 1 byte padding is necessary at the end of the definition. In the
memory it looks like this:
struct A
};
Uses of Structures
Example 1
void main( )
struct book
char name ;
float price ;
int pages ;
};
Example 2
void main( )
struct book
char name ;
float price ;
int pages ;
};
Example 3
void main( )
{
struct book
char name ;
float price ;
int pages ;
};
int i ;
Example 4
void main( )
struct employee
char name[10] ;
int age ;
float salary ;
};
struct employee e1 = { "Sanjay", 30, 5500.50 } ;
/* piece-meal copying */
e2.age = e1.age ;
e2.salary = e1.salary ;
e3 = e2 ;
Example 5
void main( )
struct address
char phone[15] ;
char city[25] ;
int pin ;
};
struct emp
char name[25] ;
struct address a ;
};
Example 6
void main( )
struct book
char name[25] ;
char author[25] ;
int callno ;
};
Example 7
struct book
char name[25] ;
char author[25] ;
int callno ;
};
void main( )
display ( b1 ) ;
Example 8
void main( )
struct book
char name[25] ;
char author[25] ;
int callno ;
};
ptr = &b1 ;
Example 9
struct book
char name[25] ;
char author[25] ;
int callno ;
};
void main( )
display ( &b1 ) ;