[go: up one dir, main page]

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

CSE 102: Computer Programming: Structures

This document provides information about structures in C programming. It defines a structure as a collection of related data items of different types. Structures allow grouping of data like employee name, age, salary. Members of a structure are accessed using the dot operator. Structures can be defined, declared, initialized, and members accessed. Arrays of structures and nested structures are also discussed.

Uploaded by

Ali Asghar
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)
58 views12 pages

CSE 102: Computer Programming: Structures

This document provides information about structures in C programming. It defines a structure as a collection of related data items of different types. Structures allow grouping of data like employee name, age, salary. Members of a structure are accessed using the dot operator. Structures can be defined, declared, initialized, and members accessed. Arrays of structures and nested structures are also discussed.

Uploaded by

Ali Asghar
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
You are on page 1/ 12

University of Engineering and Technology,

Peshawar, Pakistan

CSE 102: Computer Programming

Lecture 07
Structures

By;
Dr. Muhammad Athar Javed Sethi
Need for Structure

 Basic data type: single element


 Array: multiple elements of same type
 Task: store name, age, salary of
employees
 Different Data Types
 Structure is a collection of related
elements or data items.
 Elements of the structure are called
members of the structure.
Struct Data Type

 Treat different data types as one group


 Create new data type as a composition of
existing data types
 Field
 each element is called a “field”
 can be a basic data type or an array or another
struct
 accessed using dot (.) operator
Defining Structures

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

 Dot operator (.) is used to access the members of


a structure.
 To access a member of a specific structure, the
structure variable name, the dot operator and then
the member of the structure is written.
 Syntax;
struct_variable.struct_element
Initializing of Structure Variables
Struct address
{
char city[15];
int pcode;
};
address taq = { “Peshawar” , 25000};
Array Type Members of 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

p_data rec={{“M. Waqas”, “Haq Nawaz”, “Peshawar”,20},


{“M. Zeeshan”, “Fida Khan”,
“Islamabad”,30},6.9};

Accessing Members of Nested Structure

Rec.s1.age=20;

You might also like