0% found this document useful (0 votes)
13 views12 pages

Structure

Uploaded by

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

Structure

Uploaded by

rishinot8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structure

Ordinary variable can hold one piece of information


Array can hold a number of pieces of information of the same data type
Structure is used to deal with entities that are collection of dissimilar data types

Example: To store data about a book


Book name (String) Price (float)
Author name (String) Number of pages (Integer)
Publisher (String) etc.

Two approaches for storing name, price and number of pages of 3 books
→Using array: Three separate arrays are required for storing name, price and number of pages
for 3 books, number of arrays depend upon the number of items related to the book

→ Using structure: book_bank is the tag name


struct book_bank book1, book2, book3 are structure
{char title[20]; variables
char author[15]; Each structure variable has 4
int pages; members as specified by the template
float price;}; struct book_bank book1, book2, book3; Members of the structure themselves
are not variable, they do not occupy
any memory until they are associated
Template is terminated
with structure variables
by a semicolon
struct book_bank
struct
{char title[20];
{char title[20];
char author[15];
char author[15];
int pages;
int pages;
float price;}; book1, book2, book3;
float price;}; book1, book2, book3;
Template declaration and
variable declaration are Use of tag name is optional
combined into one statement

Use to establish a link between member and variable


[Link]
A variable to represent the price of book1
Can be treated as any other ordinary variable
Dot operator/period
operator/member operator

Assigning values to the members of book1

strcpy([Link], “Basic”); scanf(“%s”, [Link]);


[Link]=250; scanf(“%d”, &[Link]);
[Link]=28.50;
Structure initialization
A structure must be declared as static if it is to be initialized inside a function

1) 2)
main() main()
{static struct {struct st_record
{int weight; {int weight;
float height;};student={60,180.75}; float height;};
----- static struct st_record student1={60,180.75};
------} static struct st_record student2={53,170.60};
Assign 60 to [Link] and 180.75 -----
to [Link] ------}

3)
{struct st_record
{int weight;
float height;}; student1={60,180.75};
main(){
static struct st_record student2={53,170.60};
-----
------}
Arrays of structure
In analyzing the marks obtained by a class of students, a template may be used to describe
name of the students and marks obtained in various subjects. In such a situation declare all the
students as structure variables

struct class student[100];


Defines an array called student for storing 100 elements, each element is defined in the struct
class

{struct marks
{int subject1;
int subject2; 43
int subject3;}; 54
main(){ 34
82
static struct marks student[3]={{43,54,34},{82,90,43},{56,87,63}}; 90
----- 43
------} 56
87
63
Declares student as an array of 3 elements, student[0], student[1],
student[2]
student[0].subject1=43, student[0].subject2=54,
student[0].subject3=34 and so on
Student array inside memory
struct marks
{int sub1;
int sub2;
int sub3;
int total;};
main()
{int i;
static struct marks student[3] = {{45, 67, 81, 0},
{42, 53, 69, 0},
{57, 36, 71, 0}};
static struct marks total;
for (i=0;i<=2;i++)
{student[i].total=student[i].sub1+student[i].sub2+student[i].sub3;
total.sub1=total.sub1+student[i].sub1;
total.sub2=total.sub2+student[i].sub2;
total.sub3=total.sub3+student[i].sub3;
[Link]=[Link]+student[i].total;}
printf(“%d\n,%d\n,%d\n”,total.sub1,total.sub2,total.sub3);
printf(“Grand total=%d”,[Link]);}
Arrays within structure: Use of array as structure member
struct marks member subject contains 3 elements, subject[0], subject[1],
{int number; subject[2]
float subject[3];}student[2]; student[1].subject[2] refers to the marks obtained by 2nd
student in third subject
void main()
{struct marks
{int sub[3];
int total;}; static struct marks student[3] = {45, 67, 81, 0, 42, 53, 69, 0, 57, 36, 71, 0};
static struct marks total;
int i, j;
for(i=0;i<=2;i++)
{for(j=0;j<=2;j++)
{student[i].total+=student[i].sub[j]; /*per student total marks of 3 subjects*/
[Link][j]+=student[i].sub[j];} /*subject wise total*/
[Link]+=student[i].total;} /*Grand total*/
printf(“Grand total=%d”,[Link]);}
Example of structure within structure: Nesting of structures

struct salary employee is the structure variable


{char name[20]; this structure defines name, department, basic-pay and 3
char department[10]; kinds of allowances as member
int basic-pay;
int house-rent-allowance;
int dearness;
int city-allowance;};employee;

struct salary Salary structure contains a member named allowance


{char name[20]; which itself is a structure with 3 members
char department[10]; Members contained in the inner structure are
int basic-pay; house-rent, dearness, city can be referred to as
struct [Link]
{int house-rent; [Link]-rent
int dearness; [Link]
int city;};allowance;};employee;
An inner structure can have more than one variable

struct salary Inner structure has two variables,


{char name[20]; allowance and arrear, both of them have
char department[10]; the same structure template
int basic-pay; employee[1].[Link]
struct employee[1].[Link]
{int house-rent;
int dearness;
int city;};allowances, arrear;};employee[100];

Use of tag name to define inner structure

struct pay Pay template is defined outside the salary template, use to
{int dearness; define the structure of allowance and arrear inside the
int house-rent; salary structure
int city;};
struct salary
{char name[20];
char department[10];
struct pay allowance;
struct pay arrear;};
struct salary employee[100];
Structure and function
C supports the passing of structure values as arguments to the function.
Function name (Structure variable name): Format of sending a copy of structure to the called
function
Called function takes the following form
Passing of individual structure
data_type function name (st_name) element to a function
struct_type st_name; In the declaration of the structure,
{------ name and author have been declared
------- as arrays. Therefore, when we call
-------- the function display( ) using, display
return(expression);} ( [Link], [Link], [Link] ) ;
#include<stdio.h> we are passing the base addresses of
void display (char *s, char *t, int n) the arrays name and author, but the
{printf ("\n%s %s %d", s, t, n );} value stored in callno. Thus, this is a
void main( ) mixed call—a call by reference as
{struct book well as a call by value.
{char name[25]; The passing of individual elements
char author[25]; would become more tedious as the
int callno;}; number of structure elements go on
static struct book b1 = {"Let us C", "YPK", 101}; increasing. A better way would be to
display ([Link], [Link], [Link]);} pass the entire structure variable at a
time.
Output: Let us C YPK 101
Passing of entire structure to a function The calling of function display( )
#include<stdio.h> becomes quite compact, display ( b1) ;
struct book The formal argument cannot be struct
{char name[25] ; book b1 as the data type struct book is
char author[25] ; not known to the display function.
int callno ; } ; So it is necessary to define the
void display ( struct book b ) structure type struct book outside
{printf ( "\n%s %s %d", [Link], [Link], [Link] );} main(), so that it becomes known to
void main( ) all functions in the program.
{static struct book b1 = { "C program", "SM", 171 } ;
display ( b1 ) ;}

Output: C program SM 171

Union
In structure each member has its own storage location whereas all the members of a union
use the same location.
Union may contain many members of different types but it can handle only one member at
a time
Use same memory region for multiple purposes, hence occupies less memory compared to
structure
union item /*declares a variable code of type union item*/
{float x;
int m; /*compiler allocates a piece of storage that is large enough to hold the largest*/
char c;}code; /*variable type in the union*/
1000 1001 1002 1003
#include <stdio.h>
union item
{float x;
c char c;}code;
m void main()
x
{code.c=‘P’;
code.x=1243.560059;
printf("code.x=%f,code.c=%c\n",code.x,code.c);}
Output: code.x=1243.50059, code.c=?
Accessing union member: code.x, code.c, code.m
During accessing we should make sure that we are accessing the member whose value is
currently stored.
For example,
code.m=379;
code.x=7829.56;
printf(“%d”,code.m); /*error, garbage value*/
Union creates a storage location that can be used by any one of its members at a time. When a
different member is assigned a new value the new value supersedes the previous member’s
value
#include<stdio.h>
union item
{float x;
int m;char c;}code;
void main()
{code.m=379;
code.x=7829.56;
printf("code.x=%f,code.m=%d\n",code.x, code.m);}

Output: code.x=7829.560059,code.m=1173662843

#include<stdio.h>
union item
{float x;
int m;char c;}code;
void main()
{code.x=7829.56;
code.m=379;
printf("code.x=%f,code.m=%d\n",code.x,code.m);}

Output: code.x=0.000000,code.m=379

You might also like