CSE 102: Computer Programming: Structures
CSE 102: Computer Programming: Structures
Peshawar, Pakistan
Lecture 07
Structures
By;
Dr. Muhammad Athar Javed Sethi
Need for Structure
struct st_name
{
type 1; //data types
type 2;
type 3;
};
Declaring Structures
Once the structure is defined, you can declare a
structure variable by preceding the variable name
by the structure type name
struct address
{
char city[15];
int postalcode;
};
address first_var, second_var;
Accessing Members of a Structure
struct result
{
char name[15];
int sub[4];
int total;
};
result student= { “Kaleem” , {62,69,70,40}, 0};
Structure Variable as Arrays
struct result
{
char s_name[15];
int sub[4];
int total;
};
result arts[10];
Initialization of Arrays of Structure
struct marks
{
char code[10];
char name[15];
float marks;
};
marks rec[3]={{“man-1”, “Kashif”, 85.9},
{“acc-1”, “Waqas”, 89.6},
{“fac-1”, “Javed”, 55.9}};
Nested Structure
When members of a structure are defined as structure type,
these are called nested structure.
Struct info
{
char s_name[15];
char f_name[15];
char city[15];
int age;
};
Struct p_data
{
info s1;
info s2;
float x;
};
p_data rec;
Initialization of Nested Structure
Rec.s1.age=20;