0% found this document useful (0 votes)
1 views

c Programming-chapter 10

The document provides an overview of structures and unions in C programming, explaining their definitions, syntax, and operations. It covers how to declare structure variables, access members, and perform operations like copying and arithmetic on structures. Additionally, it discusses arrays of structures, nesting of structures, and the differences between structures and unions.

Uploaded by

flxv07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

c Programming-chapter 10

The document provides an overview of structures and unions in C programming, explaining their definitions, syntax, and operations. It covers how to declare structure variables, access members, and perform operations like copying and arithmetic on structures. Additionally, it discusses arrays of structures, nesting of structures, and the differences between structures and unions.

Uploaded by

flxv07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-10
STRUCTURE & UNION
What is Structure

 Structure in c is a user-defined data type that enables us to store the


collection of different data types with a single name.

 Each element of a structure is called a member. Structures can store


various information.

 The ,struct keyword is used to define the structure


Structure definition

struct structure-name
{

type member1;
type member2;
.
.
.

type member n;

}
DECLARING STRUCTURE VARIABLES
After defining a structure format we can declare variable of that structure type. It is same as like any other data type
declaration

Syntax:

struct structure-name structure-variable-name;

Ex: struct book-details book1,book2,book3;

Where,

struct is a keyword
structure-name is the name of the structure
structure-variable-name is the variable of the type structure
A member of a structure can be accessed by specifying the variable name followed by a period operator
or dot operator or member operator which in turn followed by the member name.

Syntax:

structure-variable-name. member name;


Example 1

struct student
{
int tkno,total;
} s;
void main()
{
clrscr();
printf(“Enter tkno, total\n”);
scanf(“%d”,&s.tkno);
scanf(“%d”,&s.total);
printf(“Tkno=%d\n”,s.tkno);
printf(“Total=%d\n”,s.total);
printf(“Size=%d\n”,sizeof(s));
printf(“Size=%d”,sizeof(struct student));
}
Example 2
Structure initialization in C
Structure Operations
1. Copying of structure variables
2. Comparison of two structure variables or members
3. Arithmetic operations on structures
Copying of structure variables
By using assignment operator(=) user can copy two structure variables of same type
Ex: Ex:
struct abc struct abcd
{ {
char name[20]; char name[20];
int salary,id; int salary,id;
}a,b; }c,d;
Comparison of two structure variables or members
Copying of two structure variable is allowed but comparison of two structure variables is not allowed.

For example,
a==b
a!=b
is not allowed.
Arithmetic operations on structures
Like other variables, any other operation that can be performed on members of structure variables.

For ex:

++a. salary or ++(a. salary)

++a. salary – Increment the salary before accessing its value.


a. salary++ – Increment the salary after accessing its value.
Arrays of structures
Whenever the same structure is to be applied to a group of people, items, etc., in that
situations, an array of structures will be defined.

Syntax: struct st_name st_var_name [size];

Ex:

struct student
{
int tkno;
char name[20];
};
Ex:
{
struct student scanf(“%d”,&s[i].tkno);
{ scanf(“%s”,s[i].name);
int tkno; }
char name[20]; for(i=0;i<n;i++)
}; {
printf(“Details of %d student\n”,i+1);
void main() printf(“Tkno=%d\n”,s[i].tkno);
{ printf(“Name=%s\n”,s[i].name);
srtuct student s[10]; }
int i,n; }
printf(“How many details you want to store\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
Arrays within structures

Inside the structure user can use one dimensional or two-dimensional arrays is called arrays within
structures.

Ex:

struct student
{
int number;
float mark[3];
}s[5];

Here, the member mark contains three elements, mark[0], mark[1], mark[2].
Ex: s[0]. mark[1];
Ex:
struct student {
{ s[i].total= s[i].total+s[i].mark[j];
int number, total;
int mark[3]; printf(“Mark[%d]=%d\n”,i+1,mark[j]);
}; }

Void main() printf(“Total=%d\n”, s[i].total);


{ }
struct student s[2]={101,0,78,67,87,102,0,58,96,45};
int i, j; }
for (i=0;i<2;i++)
{
printf(“Number=%d\n”, s[i]. number);
for (j=0;j<3;j++)
Structures within Structures
A Structure which includes another structure is called nesting of structure.
Syntax:

struct st_name1
{
type member1;
type member2;
struct st_name2
{
type member1;
type member2;
.
type member n;
}st_var_name;
type member n;
};
Where

 St_name1 is the outer structure name.


 St_name2 is the inner structure name.
 It is important to create inner structure variable is inside the structure definition.
 For outer structure variable user can create either inside the structure definition or in
main() function.
struct employee • Employee is the outer structure.
{ • Allowances is the inner structure.
• allw is the structure variable for inner structure
char name[20],department[20]; allowances.
• emp is the structure variable for outer structure
struct allowances employee.
{
int dearsness_allw;
int house_rent, travel_allw;
}allw;
}emp;
How to access inner structure variable
Syntax:

struct outer st_variable name. inner st_variable name. membername;


Ex:
struct emp.allw. house_rent;
Example:
#include<stdio.h>
struct student
{
char name[20];
int tno;
struct mark_details
{
int total, mark[5];
}m;
};
void main() printf(“Name=%s\n”, s.name);
{ scanf(“Token no=%d\n”,s.tno);
struct student s; for(i=0;i<5;i++)
{
s.total=0; printf(“Mark[%d]=%d\n”,i+1,s.mark[i]);
printf(“Enter name,token no and five marks\
printf(“Total=%d\n”,s.total);
n”); }
scanf(“%s”, s.name);
scanf(“%d”,&s.tno); }
for(i=0;i<5;i++)
{
scanf(“%d”,&s.mark[i]);
s.total=s.total+s.mark[i];
}
Unions
 It is similar to structure
 When a variable is associated with a union, the compiler allocates the memory by
considering the size of the largest member.
 The keyword for union is union.
Syntax: Example:
union stud
union un_name
{
{
type member 1; int tno;
type member 2;
. char st_name[20];
.
. };
type member n;
};
Difference between structure and unions
Assignment
Structure template is terminated with ______________. semicolon colon comma dot
Which among the following keyword is used to define a
structure union struct structures
structure?
The ______ is a collection of data of different data types
structure array loop switch
under a single name.
Structure is a __________type of data type userdefined derived primitive fundamental
The name of the data fields inside the structure is called structure
variables members data bank
_____________ variable
Which of the following are themselves a collection of different
string char structure array
data types?
The size of _________is equal to the size of its largest
structure enum array union
member
A __________ is a collection of different data items under
structure enum array union
one name in which the items share the same storage?
User-defined data type can be derived by___________. enum typedef struct all the above
Which operator connects the structure name to its member
:(colon) ;(semicolon) , (comma) . (dot)
name?
The member operator is also called as ___________ indirection address dot relational

Which of the following cannot be a structure member? integer character another function
structure
For accessing structure member the period operator is followed by member name structure name tag name file name
the _______
To copy one structure variable to another structure variable
_____operator is used. assignment (=) strcpy() %% ()

When you are doing partial initialization in structure the uninitialized Zero NULL void 1
float member will take the value ____
When you are doing partial initialization in structure the uninitialized Zero void 1 NULL
integer member will take the value ____
struct student {int tno;char name[10];}s1; the size of s1 is ______ 12 10 2 4
bytes
struct student {int tno;char name[10];}s1={23}; The value of the NULL('\0') 0 23 10
member name is ______
struct book{ cahr bname[20],title[20]; float price;}b1;The size of b1 is 44 20 4 40
_____bytes

A structure within a structure is called ______ nesting of union nested array recursion
structure
Which of the following method is most powerful way of tagged structure type-
user defined
defining the structure? structure variable defined
member address
The dot operator is also called as ___________ arithmetic relational
operator operator

The sizeof() operator is a ______operator unary binary arithmetic logical

Which among the following keyword is used to define a


UNIONS union struct structures
union?
All the members of the union use the __________ location in
same diiferent multiple register
memory.
1) What is structure?
2) How structures differ from an array in C language?
3) What are the 3 methods to define a structure?
4) Define a structure student with member’s tkno, name and address.
5) How do you access the members of a structure?
6) What are the elements used for declaring a structure variable?
7) What are the elements used in compile time initialization of a structure variable.
8) Explain the use of the following. A) struct B)union C)sizeof()
9) Assume that student1 and student2 are two variables of the same structure type, then how can you
copy student2 to student1?
10) Define one structure customer with the members custno, custname , custage and initialize the members
at compile time.
11) Define a structure called COMPLEX with two floating point members x and y and declare a variable p of
type COMPLEX.
12) Define one structure student with the member’s tkno, name, age and address. Write a C program to
read and display one student details using this structure.
13) What is the need for an array of structures?
14)Distinguish between arrays of structure and arrays within structure.
15)Explain nesting of structure with example.
16)Write the difference between structure and union.
17)Define a union called Book with member’s title, author and price

You might also like