2. Array vs Structure
• Array: a data structure whose elements are
all of the same data type.
• Structure: elements can differ in type.
i.e. a single structure can contain integer
elements, floating-point elements and
character elements.
Pointers, arrays, other structures can also be
included within a structure.
The individual structure elements are referred
to as members.
3. Declaring a structure
• struct keyword
• tag identifies this type of structure (structure name)
• member 1,member 2, … member m individual member
declaration
struct tag{
member 1;
member 2;
member 3;
...
member m;
};
4. Declaring a structure
• Individual member can be ordinary
variables, pointers, arrays or other
structures.
• Member names within a particular
structure must be distinct from one
another.
• Member name can be same as the
name of a variable that is defined
outside of the structure.
• Individual members can’t be initialized
within a structure type declaration.
struct tag{
member 1;
member 2;
member 3;
...
member m;
};
6. Initializing Structure
struct date{
int day;
int month;
int year;
};
struct date d={13,8,2017};
struct tag variable={value 1, value 2, ... , value m};
• Value 1 refers to the value of first member
• Value 2 refers to the value of second member
7. Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account {
int acct_no;
char acct_type;
char name[80];
float balance;
struct date lastpayment;
};
int main(){
///initializing a structure
struct account my_account={12345,'R',"Ananta",1000.50,13,8,2017};
return 0;
}
Structure member Number of bytes
acct_no 2
acct_type 1
name 80
balance 4
lastpayment 2+2+2
Total 93
8. Example Code
#include <stdio.h>
struct date{
char name[50];
int day;
int month;
int year;
};
int main(){
///declaring and initializing array of structure
struct date birthday[5]={
{"spiderman",1,1,1994},
{"superman",6,7,1994},
{"batman",15,8,1994},
{"ironman",29,2,2000},
{"hitman",8,10,1994}
};
return 0;
}
9. Example Code
struct first{
float a;
int b;
char c;
};
struct second{
char a;
float b, c;
};
• Here duplication of member names is permissible, since the
scope of each set of member definitions is confined to its
respective structure.
10. Processing a Structure
• A structure member can be accessed by writing:
variable . member;
• variable refers to the name of a structure-type
variable
• member refers to the name of a member within the
structure.
• Period (.) is an operator of the highest precedence
group and its associativity is left to right.
11. Example Code
#include <stdio.h>
struct date{
char name[50];
int day;
int month;
int year;
};
int main(){
struct date d={"Birdman",1,1,1994};
int dayno=d.day;
int monthno=d.month;
int yearno=d.year;
return 0;
}
12. Structure of Structure
variable . member. submember;
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
How to access the variable day
from the structure account
13. Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994};
int dayno=my_acc.lastpayment.day;
return 0;
}
14. Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc={123,'R',"Birdman",1000.50,1,1,1994};
char firstchar=my_acc.name[0];
return 0;
}
15. Example Code
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct account{
int acc_no;
char acc_type;
char name[50];
float balance;
struct date lastpayment;
};
int main(){
struct account my_acc1={123,'R',"Birdman",1000.50,1,1,1994};
struct account my_acc2;
my_acc2=my_acc1;///copy the values of my_acc1 to my_acc2
return 0;
}
16. References
• Programming with C, schaum’s outlines, 3rd edition
chapter 12: section 12.1 and 12.2 (programming
example 12.14)