V UNIT
Initializing Structure, Declaring structure variable – structure using typedef- Accessing members – Nested
structure- Accessing elements in a structured array – Array of structure – Accessing elements in structure
array- Passing Array of structure to function – Array of pointers to structures, Bit manipulation to structure
and pointer to structure- Union Basic and declaration – Accessing union members- Pointers to Union-
Dynamic memory allocation, malloc, realloc, free – Allocation Dynamic array- Multidimensional array
using dynamic memory allocation-file:opening, defining, closing, File modes, File types – writing
contents into a file- Reading file contents – Appending an existing file- File permissions and rights,
changing permissions and rights
STRUCTURES IN C
Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine
data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
What is a structure?
A structure is a user defined data type in C/C++. A structure creates a data type that can be
used to group items of possibly different types into a single type.
,Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows −
struct[structure tag]{
member definition;
member definition;
...
member definition;
}[one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure −
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
STRUCTURE USING TYPEDEF
Typedef is a keyword that is used to give a new symbolic name for the existing name
in a C program. This is same like defining alias for the commands.
Consider the below structure.
struct student
{
int mark [2];
char name [10];
float average;
}
Variable for the above structure can be declared in two ways.
1st way :
struct student record; /* for normal variable */
struct student *record; /* for pointer variable */
2nd way :Typedef struct student status;
When we use “typedef” keyword before struct<tag_name> like above, after that we can
simply use type definition “status” in the C program to declare structure variable.
Now, structure variable declaration will be, “status record”.
This is equal to “struct student record”. Type definition for “struct student” is status. i.e.
status = “struct student”
AN ALTERNATIVE WAY FOR STRUCTURE DECLARATION USING TYPEDEF IN
C:
typedef struct student
{
int mark [2];
char name [10];
float average;
} status;
To declare structure variable, we can use the below statements.
status record1; /* record 1 is structure variable */
status record2; /* record 2 is structure variable */
EXAMPLE PROGRAM FOR C TYPEDEF
// Structure using typedef:
, #include <stdio.h>
#include <string.h>
typedef struct student
{
int id;
char name[20];
float percentage;
} status;
int main()
{
status record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
OUTPUT:
id is: 1
Name is: Raju
Percentage is: 86.500000
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ACCESSING STRUCTURE MEMBERS
To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access. You would use the keyword struct to define variables of
structure type. The following example shows how to use a structure in a program −
#include<stdio.h>
#include<string.h>
struct Books{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(){
Initializing Structure, Declaring structure variable – structure using typedef- Accessing members – Nested
structure- Accessing elements in a structured array – Array of structure – Accessing elements in structure
array- Passing Array of structure to function – Array of pointers to structures, Bit manipulation to structure
and pointer to structure- Union Basic and declaration – Accessing union members- Pointers to Union-
Dynamic memory allocation, malloc, realloc, free – Allocation Dynamic array- Multidimensional array
using dynamic memory allocation-file:opening, defining, closing, File modes, File types – writing
contents into a file- Reading file contents – Appending an existing file- File permissions and rights,
changing permissions and rights
STRUCTURES IN C
Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine
data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −
Title
Author
Subject
Book ID
What is a structure?
A structure is a user defined data type in C/C++. A structure creates a data type that can be
used to group items of possibly different types into a single type.
,Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows −
struct[structure tag]{
member definition;
member definition;
...
member definition;
}[one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure −
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
,@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
STRUCTURE USING TYPEDEF
Typedef is a keyword that is used to give a new symbolic name for the existing name
in a C program. This is same like defining alias for the commands.
Consider the below structure.
struct student
{
int mark [2];
char name [10];
float average;
}
Variable for the above structure can be declared in two ways.
1st way :
struct student record; /* for normal variable */
struct student *record; /* for pointer variable */
2nd way :Typedef struct student status;
When we use “typedef” keyword before struct<tag_name> like above, after that we can
simply use type definition “status” in the C program to declare structure variable.
Now, structure variable declaration will be, “status record”.
This is equal to “struct student record”. Type definition for “struct student” is status. i.e.
status = “struct student”
AN ALTERNATIVE WAY FOR STRUCTURE DECLARATION USING TYPEDEF IN
C:
typedef struct student
{
int mark [2];
char name [10];
float average;
} status;
To declare structure variable, we can use the below statements.
status record1; /* record 1 is structure variable */
status record2; /* record 2 is structure variable */
EXAMPLE PROGRAM FOR C TYPEDEF
// Structure using typedef:
, #include <stdio.h>
#include <string.h>
typedef struct student
{
int id;
char name[20];
float percentage;
} status;
int main()
{
status record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
OUTPUT:
id is: 1
Name is: Raju
Percentage is: 86.500000
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ACCESSING STRUCTURE MEMBERS
To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access. You would use the keyword struct to define variables of
structure type. The following example shows how to use a structure in a program −
#include<stdio.h>
#include<string.h>
struct Books{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(){