WINSEM2023-24_BCSE102L_TH_VL2023240501147_2024-02-09_Reference-Material-I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

MODULE 4

STRUCTURES AND UNION


struct employee
{ int id;
char name[20];
float salary;
};
Memory allocation
Declaring structure variable

struct employee
{ int id;
char name[50];
float salary;
};
• Now write given code inside the main()
function.
• struct employee e1, e2;
Declaring structure variable

struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
Which approach is good

• If number of variables are not fixed, use the


1st approach. It provides you the flexibility to
declare the structure variable many times.
• If no. of variables are fixed, use 2nd approach.
It saves your code to declare a variable in
main() function.
Accessing members of the structure

• There are two ways to access structure


members:
• By . (member or dot operator)
• By -> (structure pointer operator)
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};

int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;

// Assign values to members of s1


s1.myNum = 13;
s1.myLetter = 'B';

// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);

return 0;
}
int main() {
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
struct myStructure s1;

// Trying to assign a value to the string


s1.myString = "Some text"; // An error will occur:

// Trying to print the value


printf("My string: %s", s1.myString);

return 0;
}
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};

int main() {
struct myStructure s1;

// Assign a value to the string using the strcpy function


strcpy(s1.myString, "Some text");

// Print the value


printf("My string: %s", s1.myString);

return 0;
}
struct Car {
char brand[50];
char model[50];
int year;
};
int main() {
struct Car car1 = {"BMW", "X5", 1999};
struct Car car2 = {"Ford", "Mustang", 1969};
struct Car car3 = {"Toyota", "Corolla", 2011};
printf("%s %s %d\n", car1.brand, car1.model, car1.year);
printf("%s %s %d\n", car2.brand, car2.model, car2.year);
printf("%s %s %d\n", car3.brand, car3.model, car3.year);
return 0;
}
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};

// Create another structure variable


struct myStructure s2;

// Copy s1 values to s2
s2 = s1;

// Change s2 values
s2.myNum = 30;
s2.myLetter = 'C';
strcpy(s2.myString, "Something else");

// Print values
printf("%d %c %s\n", s1.myNum, s1.myLetter, s1.myString);
printf("%d %c %s\n", s2.myNum, s2.myLetter, s2.myString);
• Initialize Structure Members
• Structure members cannot be initialized with
the declaration. For example, the following C
program fails in the compilation.
struct Point {
int x = 0; // COMPILER ERROR: cannot initialize
members here
int y = 0; // COMPILER ERROR: cannot initialize
members here };
• The reason for the above error is simple.
When a datatype is declared, no memory is
allocated for it. Memory is allocated only
when variables are created.
• We can initialize structure members in 3 ways
which are as follows:
• Using Assignment Operator.
• Using Initializer List.
• Using Designated Initializer List.
1. Initialization using Assignment
Operator
• struct structure_name str;
• str.member1 = value1;
• str.member2 = value2;
• str.member3 = value3; . . .
• 2. Initialization using Initializer List
• struct structure_name str = { value1, value2,
value3 };
• In this type of initialization, the values are
assigned in sequential order as they are
declared in the structure template.
• 3. Initialization using Designated Initializer List
• Designated Initialization allows structure members to
be initialized in any order. This feature has been added
in the C99 standard.
struct structure_name str = {
.member1 = value1,
.member2 = value2,
.member3 = value3 };

The Designated Initialization is only supported in C but


not in C++.
no empty spaces in between the
members of the structure
These unused bytes or holes between boundaries of
the members of structure are known as slack bytes.
SLACK BYTES:
The optimized compilers will always assign even addresses to the
members of the structure so that the data can be accessed very fast.
The even addresses may be multiples of 2, 4, 8 or 16. This introduces
some unused bytes or holes between the boundaries of some
members. These unused bytes or holes between boundaries of the
members of structure are known as slack bytes.
The slack bytes do not contain any useful information and they are
actually a wastage of memory space. But the access of the data would
be much faster in the case of slack bytes concept. When the slack
bytes are used, the size of the structure would be greater or the same
as the sum of the size of the individual members of the structure.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}

You might also like