Lec10 (Structure Array)
Lec10 (Structure Array)
CONTENTS
STRUCTURE
STRUCTURE DEFINITION
GIVING VALUES TO MEMBERS
STRUCTURE INITIALIZATION
ARRAYS OF STRUCTURE
UNION
STUCTURE
Example:
struct name {
char title[20];
int pages;
};
Advantages of structure
GENERAL FORMAT
struct tag {
member 1;
member 2;
…………..
member m;
};
KEY POINTS
struct is a required keyword.
struct account {
int acc_no;
char acc_type;
char name[80];
float balance;
};
#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
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;
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
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
struct name {
int weight;
float height;
}
main(){
static struct name student={53, 170, 69};
}
ARRAYS 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:
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
{
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;
}