Lec10 (Structure Array)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

STRUCTUEES AND UNION

CONTENTS
 STRUCTURE
 STRUCTURE DEFINITION
 GIVING VALUES TO MEMBERS
 STRUCTURE INITIALIZATION
 ARRAYS OF STRUCTURE
 UNION
STUCTURE

STRUCTURE: A structure is an aggregate data type that


is composed of two or more related variables called
members.

Unlike an array in which each element is of the same


type, each member of a structure have its own type,
which may be differ from the types of the other
members.
DIFFERENCE BETWEEN STRUCTURE AND ARRAY
Array can be used to represent a group of data items
that belong to the same type and same name.

Example : int name[10];

By using structure, a collection of data items of different


types can be represented using single name.

Example:
struct name {
char title[20];
int pages;
};
Advantages of structure

 A structure is a convenient tool for handling a group of


logically related data items.

 Structures help to organize complex data in a more


meaningful way.

 It is a powerful concept that we may often need to use in


our program design.
DEFINING A STRUCTURE

Structure declarations are somewhat more complicated than


array declaration, since a structure must be defined in terms of
its individual members.

GENERAL FORMAT

struct tag {
member 1;
member 2;
…………..
member m;
};
KEY POINTS
 struct is a required keyword.

 tag is a name that identifies structures of this type.

 member1, member2 and member m are individual member


declaration.

 individual members can be ordinary variables, pointers,


array.

 the members name within a particular structure must be


distinct from one another.
STRUCTURE DECLARATION:

struct account {
int acc_no;
char acc_type;
char name[80];
float balance;
};

Here structure is named account( tag is account).

It contains four members.


STRUCTURE DECLARATION:

#include<stdio.h>
#include<string.h>
int main ()
{
struct student
{
int roll;
char *name[40];
};
struct student one, two;
one.roll=2008001;
one.name= "Fahim Faisal";
printf("Roll:%d\n",one.roll);
printf("Name:%s\n", one.name);

return 0;
}
account

acc_no contain
acc_no
2byte

acc_type contain
1 byte acc_type

name contain 80
byte
name[80]
balance contain 4
byte

balance
account contain
87 byte in a
memory
STRUCTURE DEFINITION

A structure definition creates a format that may be used to


declare structure variables.

General format:
struct tag_name {
data_type member1;
data_type member2;
data_type member3;
……………………………
} variable_list;
KEY POINTS:
 The keyword struct tells the compiler that a structure
type is being defined.
 The tag_name is essentially the type name of the
structure.
 Variabele_list is where actual instances of the structure
are declared.
 Either tag_name or variable list is optional, but one must
be present.
EXAMPLE struct book_bank {
char title[20];
char author [15];
int pages;
}
struct book_bank book1, book2, book3;

Declares book1, book2 and book3 as variables of type


book_bank.

Each of these variables have four members as specified


by the template.
EXAMPLE Struct book_bank {
char title[20];
char author [15];
int pages;
}
book1, book2, book3;

The use of tag_name is optional.


Struct {
char title[20];
char author [15];
int pages;
}
book1, book2, book3;
GIVING VALUES TO MEMBERS

 We can assign to the members of a structure in


numbers of way.

 The link between a member and a variable is


established using the member operator ‘.’

 The members should be linked to the structure


variables in order to make them meaningful
members.
EXAMPLE
book1.price

Is the variable representing the price of the book1 and


can be treated like any other ordinary variable.

book1.price=28.50; book1.pages=250;
book1.chapter=10;
We can also scanf to give the values through the
keyboard.
scanf(“%d”,&book1.price);
Scanf(“%d”,&book1.pages);
scanf(“%d”, &book1.chapter);
PROGRAM: Defining and Assigning values to structure
members.
struct personal {
char name[20];
int day;
char month[10];
output
int year;
Input values
float salary; Tamim 10 july 1945 4500
}; Tamim 10 july 1945 4500
int main(){
struct personal p;
printf(“input values\n:”);
scanf(“%s%d%s%d
%f”,&p.name,&p.day,&p.month,&p.year,&p.salary);
printf(“%s%d%s%d%f\
n”,p.name,p.day,p.month,p.year,p.salary);
}
STRUCTURE INITIALIZATION

A structure must be declared as static if it is to be initialized


inside a function.

main(){
static struct {
int weight;
float height;
}
student={60,18.75};
}
This assign the value to 60 to student. weight and 18.75 to
student. height.
Another method

Another method is to initialize a structure variable


outside the function as follows:

struct name {

int weight;
float height;
}
main(){
static struct name student={53, 170, 69};
}
ARRAYS OF STRUCTURE

We may declare an array of structures, each element of the


array representing a structure variable.

struct class student[100];

Defines an array called student, that consists of 100 elements.


Each element is defined to be of the type struct class.
Array of structure

my_struct st[3];

st[0].name=”TULIP”;
st[0].marks=750;
st[0].cgpa=3.25;

st[1].name=”ROSE”;
st[1].marks=812;
st[1].cgpa=3.85;
UNION
Union like structures, contain members whose individual
data types may differ from one another.

USEGES:

 access individual bytes of larger type

 sharing an area to save storage usage


GENERAL FORMAT

union tag{
member 1;
member 2;

member m;
};
Where union is a required keyword and the other terms have
the same meaning as in a structure definition.
EXAMPLE

union tag{
int a1;
float b;
char c;
};
DIFFERENCE BETWEEN UNION AND STRUCTURE

In structures each member has its own storage location,


whereas all the members of a union use the same
location.

 Although a union may contain many members of


different types, it can handle only one member at a time.

Union are used to conserve memory.


#include <stdio.h>
Structure Memory Allocation
struct s1

{
int n;
double d;
char c;
};

struct s2
{
char c;
int n;
double d;
};

int main(){
printf("char size : %lu bytes\n",
sizeof(char));
printf("int size : %lu bytes\n",
sizeof(int));
printf("double size : %lu bytes\n",
sizeof(double));
printf("s1 size : %lu bytes\n",
sizeof(struct s1));
printf("s2 size : %lu bytes\n",
sizeof(struct s2));
Union Memory Allocation

#include<stdio.h>
union example{
char ch;
int x;
int y;
} uexample;
int main(){
uexample.ch='M';
printf("uexample.ch = %c\n", uexample.ch);
Output:
uexample.x=20;
printf("uexample.x = %d\n", uexample.x); uexample.ch = M
printf("uexample.ch = %c\n", uexample.ch); uexample.x = 20
uexample.y=22; uexample.ch =
printf("uexample.y = %d\n", uexample.y); uexample.y = 22
printf("uexample.x = %d\n", uexample.x); uexample.x = 22
printf("uexample.ch = %c\n", uexample.ch); uexample.ch =
return 0;
}

You might also like