UNIT - 3 (Structures & Union) : Syntax
UNIT - 3 (Structures & Union) : Syntax
C Structure is a collection of different data types which are grouped together and each element in a C structure
is called member.
a = 10;
Example b = “Hello”;
C Variable:
Syntax: int a;
int Example: a = 20;
Syntax: char b;
char Example: b=’Z’;
C Array:
Syntax: int a[3];
Example:
a[0] = 10;
a[1] = 20;
a[2] = 30;
int a[3] = ‘\0’;
Syntax: Syntax:
struct tag_name struct tag_name
{ {
data type var_name1; data type var_name1;
data type var_name2; data type var_name2;
data type var_name3; data type var_name3;
}; };
Example: Example:
struct student struct student
{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
}; };
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record = {0}; //Initializing to null
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
USES OF STRUCTURES IN C:
1. C Structures can be used to store huge data. Structures act as a database.
2. C Structures can be used to send data to the printer.
3. C Structures can interact with keyboard and mouse to store the data.
4. C Structures can be used in drawing and floppy formatting.
5. C Structures can be used to clear output screen contents.
6. C Structures can be used to check computer’s memory size etc.
C – Array of Structures
As you know, C Structure is collection of different datatypes (variables) which are grouped together. Whereas,
array of structures is nothing but collection of structures. This is also called as structure array in C.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000
C – Passing struct to function
A structure can be passed to any function from main function or from any sub function.
Structure definition will be available within the function only.
It won’t be available to other functions unless it is passed to those functions by value or by
address(reference).
Else, we have to declare structure variable as global variable. That means, structure variable should be
declared outside the main function. So, this structure will be visible to all the functions in a C program.
PASSING STRUCTURE TO FUNCTION IN C:
It can be done in below 3 ways.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
Id is: 1
Name is: Raju
Percentage is: 86.500000
EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS:
In this program, the whole structure is passed to another function by address. It means only the address of the
structure is passed to another function. The whole structure is not passed to another function with all members
and their values. So, this structure can be accessed from called function by its address.
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0;
}
Id is: 1
Name is: Raju
Percentage is: 86.500000
C – Nested Structure
Nested structure in C is nothing but structure within structure. One structure can be declared inside other
structure as we declare structure members inside a structure.
The structure variables can be a normal structure variable or a pointer variable to access the data. You can
learn below concepts in this section.
1. Structure within structure in C using normal variable
2. Structure within structure in C using pointer variable
1. STRUCTURE WITHIN STRUCTURE IN C USING NORMAL VARIABLE:
This program explains how to use structure within structure in C using normal variable.
“student_college_detail’ structure is declared inside “student detail” structure in this program. Both
structure variables are normal structure variables.
Please note that members of “student_college_detail” structure are accessed by 2 dot(.) operator
and members of “student detail” structure are accessed by single dot(.) operator.
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data;
int main()
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"Anna University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
Id is: 1
Name is: Raju
Percentage is: 90.500000
Union and structure in C are same in concepts, except allocating memory for their members.
Structure allocates storage space for all its members separately.
Whereas, Union allocates one common storage space for all its members
We can access only one member of union at a time. We can’t access all member values at the same time in
union. But, structure can access all member values at the same time. This is because, Union allocates one
common storage space for all its members. Where as Structure allocates storage space for all its members
separately.
Many union variables can be created in a program and memory will be allocated for each union variable
separately.
Below table will help you how to form a C union, declare a union, initializing and accessing the members of
the union.
Syntax: Syntax:
union tag_name union tag_name
{ {
data type var_name1; data type var_name1;
data type var_name2; data type var_name2;
data type var_name3; data type var_name3;
}; };
Example: Example:
union student union student
{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
}; };
Declaring union using normal variable: Declaring union using pointer variable:
union student report; union student *report, rep;
Accessing union members using normal Accessing union members using pointer
variable: variable:
report.mark; report -> mark;
report.name; report -> name;
report.average; report -> average;
EXAMPLE PROGRAM FOR C UNION:
#include <stdio.h>
#include <string.h>
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}
OUTPUT:
If we want to access all member values using union, we have to access the member before assigning values
to other members as shown in record2 union variable in this program.
Each union members are accessed in record2 example immediately after assigning values to them.
If we don’t access them before assigning values to other member, member name and value will be over
written by other member as all members are using same memory.
We can’t access all members in union at same time but structure can do that.
DIFFERENCE BETWEEN STRUCTURE AND UNION IN C:
C Structure C Union
Structure allocates storage space Union allocates one common storage space for all its members.
for all its members separately. Union finds that which of its member needs high storage space over
other members and allocates that much space
C – 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;
}
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;
#include <stdio.h>
#include <string.h>
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
Typedef can be used to simplify the real commands as per our need.
For example, consider below statement.
typedef long long int LLI;
In above statement, LLI is the type definition for the real C command “long long int”. We can use type
definition LLI instead of using full command “long long int” in a C program once it is defined.