0% found this document useful (0 votes)
25 views17 pages

Unit 3 - CH 3. Structures

The document discusses C programming structures. Structures allow grouping different data types together to represent complex data like student records with a name, ID, and GPA. The structure defines a new user-defined data type that can then be used to declare structure variables. Structure members are accessed using the dot operator. Arrays of structures and initializing structures are also covered.

Uploaded by

Manjunatha H R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views17 pages

Unit 3 - CH 3. Structures

The document discusses C programming structures. Structures allow grouping different data types together to represent complex data like student records with a name, ID, and GPA. The structure defines a new user-defined data type that can then be used to declare structure variables. Structure members are accessed using the dot operator. Arrays of structures and initializing structures are also covered.

Uploaded by

Manjunatha H R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Structures

C PROGRAMMING  UNIT 4>STRUCTURES


Structures 2
 Arrays supports packing same type data together.
 Many times packing of different types of data is necessary in
programming.
 Student record example.
 How to store and process details of 60 students in a class?? (e.g:
name,id,cgpa)
char names[60][25];
int id;
int cgpa[60];
Structures 3

 Structures in C: Provides a better way to deal with such situation.


 Structures enable us to create/define new data types..!!
called as ‘User Defined Data types’.
 Can combine different data types to define these new data types.
 Individual elements are called “members”.
 Structure are also called records.
Structures 4

 General format:
struct type-name
{
data-type1 name1;
data-type2 name2;
------------- ---------
------------- ---------
};
Structures : Defining student structure 5

 For our student problem:


Name of new
data type
struct Student
keyword {
char name[25]; Constituent data of
int id; the new data type

float cgpa;
};

 Now Student data type behaves similar to built-in data types like
int, char etc.
Array vs. Structure 6

Array Structure
 Collection of related data of  Collection of different data
same type. types.
 A derived data type from  A programmer/user defined
existing data types(e.g int data type.
int[ ])  Need to define data type first,
 Data type is often available, then declare variables/arrays of
just declare array the new type.
Declaring Structure variables 7
 Assume the structure is defined as:
struct mytype
{
int n;
char c;
float f;
};
 Declaring variable of type mytype.
struct mytype s1,s2;
Declaring structure variables-other way 8

struct mytype
{
int n;
char c;
float f;
}s1,s2;

 mytype is the new data type and s1,s2 are variables(instances)


of that type!!
Accessing structure members 9
struct mytype
{
int n;
char c;
float f;
}s1;
void main(){
s1.n = 10;
s1.c = ‘a’;
s1.f = 1.32;
printf("%d %c %f", s1.n, s1.c,s1.f);
Structure Initialization
10
Student structure
11
Student structure
12
Array of Structures 13

 Array of structures is collection of structures.


 Also called as structure array
 Syntax:
struct type_name identifier[size-value];
Eg: struct Student s[10];
14
Copying and Comparing Structure
15
variables

 2 variables of the same structure type can be copied the same way as
ordinary variables.
 E.g. : if person1 and person2 belong to same structure, then:
 person1=person2;
 person2=person1;
 C doesn’t permit logical operations on structure variables.
 To compare structure variables , we do so by comparing members
individually.
16
Word Boundaries and Slack bytes 17
 Computer stores structures using “word boundary”.
 The size of word boundary is machine dependent.
 In a computer with 2 bytes word boundary, the members of a structure are
stored left_aligned on the word boundary.
 A character data takes one byte and an integer takes 2 bytes.one byte between
them is unoccupied. This unoccupied byte is known as slack byte.
 Because of the slack byte values C does not support structure comparison

0123 1 2 3

You might also like