Module 9 - Structures
Module 9 - Structures
• Tuple Data
• Structures
• Nested Structures and Array of Structures
• Passing Structure Variables to Functions
• Union
• Enumerator
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Tuple Data
Tuple Data
Consider student database. Every student has a few attributes:
• Name
• ID
• Group
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Representing Tuple Data
First attempt: 3 lists (i.e. arrays)
– Name array, ID array, and Group array.
– Problems: Consider add / delete operations.
• Shifting to be done in 3 arrays separately.
• 3 arrays are the 3 element values passed as
parameters.
• Data representation “does not say” the 3 things are
related.
Structures in C
Structures in C
• Structure
– Group of logically related data items
– Example: ID, Name, Group
• A single structure may contain data of one or more data types
– ID: int ID
– Name: char Name[]
– Group: char Group[]
• The individual structure elements are called members
• We are essentially defining a new data type
• Then we can create variables of the new data type
– These variables can be global, static or auto
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Defining a Structure
struct struct_name { Example: Student structure
member 1; containing ID, Name, Group
member 2;
… struct student {
}; int ID;
char Name[20]; Members
• struct is the required
char Group[3];
keyword
• struct_name is the name of }
the structure
• member 1, member 2, …
are individual member
declarations
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Declaring Variables of the
Structure type
struct struct_name var_1, var_2, …, var_n;
Example 1: Example 2:
struct student { struct book {
int ID; char Name[20];
char Name[20]; float price;
char Group[3]; char ISBN[30];
}; };
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Combining Structure Definition
and Variable Definition
struct struct_name{ struct {
data_type member 1; data_type member 1;
data_type member 2; data_type member 2;
… …
}var1, var2, … varn; }var1, var2, … varn;
Example: Example:
struct student { struct {
int ID; int ID;
char Name[20]; char Name[20];
char Group[3]; char Group[3];
} s1, s2; } s1, s2;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Accessing members of a
structure
• A structure member can be accessed by writing
structure_variable.MemberName
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Where can we define
structures?
• Structures can be defined in:
• Global Space
• Outside all function definitions
• Available everywhere inside and outside all functions to
declare variables of its type
• Local Space
• Inside a function
• Variables of this structure type can be declared and
used only inside that function.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Examples
Structure defined in global space: Structure defined in local space:
#include <stdio.h> #include <stdio.h>
struct student{ int f1() {
int ID; struct student{
char Name[20]; int ID;
float marks[5]; char Name[20];
}; float marks[5];
int f1() { };
struct student s1; struct student s3;
… // operations on s1 … // operations on s3
} }
int f2() { int f2() {
struct student s2; // can’t define a variable
… // operations on s2 with struct student here
} }
int main() { int main() {
… …
} }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Structure Initialization
struct book {
char Name[20];
float price;
char ISBN[30];
};
struct book b1 = {”Book1”, 550.00, ”I1”},
b2 = {”Book2”, 650.00, ”I2”};
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
typedef
Allows users to define new data-types
Syntax:
typedef type new-type
Example 1:
typedef int Integer;
Integer I1, I2;
Example 2:
typedef struct{
float real;
• It is a common practice to define
float imag; structures using typedef
} COMPLEX;
• It simplifies the syntax and increases
readability of the program
COMPLEX c1, c2;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
sizeof() for struct variables
struct test_struct {
short a; //2bytes printf("a= %lu\n", sizeof(test.a)); = 2
int b; //4bytes printf("b= %lu\n", sizeof(test.b)); = 4
char c; //1byte printf("c= %lu\n", sizeof(test.c)); = 1
} test; printf("%lu\n", sizeof(test)); = 12
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Memory view of storing a
struct variable
struct test_struct { Address
short a; //2bytes
int b; //4bytes a (2 bytes) 1000
char c; //1byte 1001
padding (2 bytes)
1002
} test;
1003
1004
b (4 bytes) 1005
1006
1007
c (1 byte)
1008
1009
Note: This illustration considers that each padding (3 bytes) 1010
address is represented by 4 bits. 1011
• We can notice that the padding added by compiler is less (only 1 byte), for the
same set of variable types, arranged with a different order in the structure.
• The total number of bytes to be added for padding is decided by the compiler
depending upon the arrangement of members inside the structure.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Operations on struct variables
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Swap two structure variables
struct book { Output:
char Name[20]; The name of the Book in b1 is Programming
float price; The name of the Book in b2 is Algorithms
char ISBN[30];
};
int main(){
struct book b1 = {”Algorithms”, 550.00, ”I1”};
struct book b2 = {”Programming”, 650.00, ”I2”};
// swapping b1 and b2
struct book b3;
b3 = b1;
b1 = b2;
b2 = b3;
printf(”The name of Book in b1 is %s\n”, b1.Name);
printf(”The name of Book in b2 is %s\n”, b2.Name);
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Incorrect way of using structure
variables
struct book {
char Name[20];
float price;
char ISBN[30];
};
int main(){
struct book b1 = {”Algorithms”, 550.00, ”I1”};
struct book b2 = {”Programming”, 650.00, ”I2”};
// Incorrect way of using structure variables
// if (b1 == b2)
if(b1.price == b2.price) // Correct way
printf(”Both the books have the same price\n”);
else
printf(”Both the books DON’T have the same price”);
return 0;
} Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
Program to store information of 5 students
int main(){
#include <stdio.h> printf(“Enter the details:\n”);
for(i = 0; i < 5; i++)
struct student{ {
char Name[10]; printf(“Enter name\n”);
float CGPA; scanf(“%s”, s[i].name);
float marks[5]; printf(“Enter the marks\n”);
}; for(j = 0; j< 5; j++)
scanf(“%f”,&s[i].marks[j]);
struct student s[5]; printf(“Enter the CGPA\n”);
scanf(“%f”,&s[i].CGPA);
}}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Storing database records in
Array of Structures
• Remember our motivation for structures to store tuple data…
ID Name Group
0910 Rama Sarma B4
0313 Alex Mathew A8
0542 Vijay Kumar A7
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example – Pass by value
return (t)
x.re = a.re, x.im = a.im
y.re = b.re, y.im = b.im
c = t
c.re t.re
c.im t.im
t.re x.re + y.re
t.im x.im + y.im
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Previous Example (modified)
Program to store information void computeSum(struct student[], int);
of 5 students, compute total int main(){
marks for each student and printf(“Enter the details:\n”);
print it. for(i = 0; i < 5; i++)
{
#include <stdio.h> printf(“Enter name\n”);
scanf(“%s”, s[i].name);
printf(“Enter the marks\n”);
struct student{
for(j = 0; j< 5; j++)
char Name[10];
scanf(“%f”,&s[i].marks[j]);
float CGPA; printf(“Enter the CGPA\n”);
float marks[5]; scanf(“%f”,&s[i].CGPA);
}; }
computeSum(s,5);
struct student s[5]; }
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example (contd.)
void computeSum(struct student sArr[], int size){
int i,j,sum;
for(i=0;i<size;i++)
{
sum = 0;
for(j=0;j<5;j++)
{
sum += sArr[i].marks[j];
}
printf(“Total marks of %s are %d”, sArr[i].Name, sum);
}
return;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Enumerator in C
Enumerator in C
• Consider this structure • However, we know that there
definition: are only limited number of
struct student { groups (or disciplines) offered
int ID; in our Campus. They include:-
{A1, A2, A3, A4, A5, A7, A8,
char Name[20]; B1, B2, B3, B4, B5, D2}
char Group[3];
}; • Can we do something to
restrict the values assigned to
• The member Group of the the member Group to always
above structure has been be from the above set?
declared as a character
array of 3 characters, which • enum is the solution!
can take any values.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Enumerator in C
typedef enum {A1,A2,A3,A4,A5,A7,A8,B1,B2,B3,B4,B5,D2}
Group_lbl;
struct student {
int ID;
char Name[20];
Group_lbl Group;
};
int main(){
struct student s1 = {876, “Karthik”, A1};
struct student s2;
s2.ID = 233;
s2.Name = “Ganesh”;
s2.Group = B5;
if(s2.Group == B5) printf(“s2 is studying Physics”);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus