DS Class 5-Strctures-AI&ML (2)
DS Class 5-Strctures-AI&ML (2)
• Use the struct keyword inside the main() method, followed by the
name of the structure and then the name of the structure variable:
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
Access Structure Members
.
use the dot syntax ( )
#include <stdio.h>
s1.myNum = 13;
s1.myLetter = 'B';
return 0;
}
create multiple structure variables with
different values, using just one structure:
s2.myNum = 20;
s2.myLetter = 'C';
Strings in Structures?
#include <stdio.h>
#include <string.h>
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");
printf("My string: %s", s1.myString); // Print the value
return 0;
}
Copy Structures
int main() {
struct myStructure s1 = {13, 'B', "Some text"};
s1.myNum = 30; // Modify values
s1.myLetter = 'C';
strcpy(s1.myString, "Something else");
// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);
return 0;
}
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};
// 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);
Use a structure to store different information about Cars:
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};
return 0;
}
Structure Comparison
int humanequals(humanbeing person1,humanbeing person2)
{
if(strcmp(person1.name==person2.name))
return 0;
if(person1.age!=person2.age)
return 0;
if(person1.salary!=person2.salary)
return 0;
return 1;
}
Structures and
pointers
• C structures can be accessed in 2 ways
1. using normal structure variables
2. Access structure members using pointer
#include<stdio.h>
struct employee
{
char name[20];
int age;
float salary;
};
void main( )
{ e
printf("enter salary\n"); p
scanf("%f", &p->salary);
printf("name is %s\n",p->name);
printf("age is %d\n",p->age);
printf("salary is %f\n",p->salary);
}
Nested structures(structure within
structures)
• A structure can be member of another structure. This is called as a nested structure
struct date
{ int dd;
int mm;
int yyyy;
};
Nested structures(structure within
structures)
The above structure date can be used in the structure employee as
struct employee
{
char name[20];
int emp_id;
float salary;
struct date doj; //nested structure
};
• In the above example, the date structure is nested within
employee structure.
• In order to access the member of a date structure, we need to
first create a variable of employee structure.
struct node { n
int data;
struct node* link; data
}; link
int main()
{
struct node n;
return 0;
}
Array of structure
//time consuming
• To store the information of 10 students, the structure variable has to
be declared as follows:
struct student s[10];
Syntax:
union tagname{
type1 member1;
type2 member2;
……
};
Examples:
union item struct item
{ {
int a; int a;
float b; float b;
char x; char x;
}u; }s;
sizeof(u)=? sizeof(s)=?
Example1: union
union un
{
short a;
short b;
}; output: ?
void main(){
union un var;
var.a=10;
printf(“%d”,var.b); 10 20
var.b=20;
printf(“%d”,var.a);
}
Example2: union
void main() x.grade=‘A’;
{ printf(“grade=%c”,x.grade);
typedef union x.per=90.5;
{ printf(“percentage=%f”,x.per);
int marks; }
char grade;
float per; Output: ?
}student;
student x;
x.marks=100;
printf(“marks=%d”,x.marks);
Differences between structures
and unions
Structure Union
The keyword struct is used to define a The keyword union is used to define a
structure union
When a variable is associated with a When a variable is associated with a
structure the compiler allocate the union, the compiler allocates the
memory for each member. The sizeof memory by considering the size of the
structure is greater than or equal to largest member, so size of union is
the sum of sizes of its member. equal to the sizeof largest member
Altering the value of a member will Altering the value of any of the
not affect other members of the member will alter other member
structure values.
Individual members can be accessed at Only one member can be accessed at a
any time, separate memory is reserved time since memory is shared by each
for each member member