0% found this document useful (0 votes)
2 views

DS Class 5-Strctures-AI&ML (2)

The document explains structures (structs) in C programming, which are user-defined data types that group different types of data into a single type. It covers how to create, access, and modify structures, including nested structures and self-referential structures, as well as the differences between structures and unions. Additionally, it provides examples of using structures to store information about cars and students, and discusses accessing structure members using both normal variables and pointers.

Uploaded by

basavananda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS Class 5-Strctures-AI&ML (2)

The document explains structures (structs) in C programming, which are user-defined data types that group different types of data into a single type. It covers how to create, access, and modify structures, including nested structures and self-referential structures, as well as the differences between structures and unions. Additionally, it provides examples of using structures to store information about cars and students, and discusses accessing structure members using both normal variables and pointers.

Uploaded by

basavananda
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Structures (structs)

• The structure is a user-defined data type that can be


used to group items of possibly different types into a
single type. The struct keyword is used to define the
structure in the C programming language.

• The items in the structure are called its member and


they can be of any valid data type.

• Additionally, the values of a structure are stored in


contiguous memory locations.
Create a Structure
• Create a structure by using the struct keyword and declare
each of its members inside curly braces

• struct MyStructure { // Structure


declaration
int myNum; // Member (int
variable)
char myLetter; // Member (char
variable)
}; // End the structure with a semicolon
To access the structure, create a variable of it:

• 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

To access members of a structure,

.
use the dot syntax ( )
#include <stdio.h>

struct myStructure { // Create a structure called 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';

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


printf("My letter: %c\n", s1.myLetter);

return 0;
}
create multiple structure variables with
different values, using just one structure:

// Create different struct variables


struct myStructure s1;
struct myStructure s2;

// Assign values to different struct variables


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

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

• Assign one structure to another.

• struct myStructure s1 = {13, 'B', "Some text"};


struct myStructure s2;
s2 = s1;
Modify Values
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};

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"};

// 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);
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};

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;
}
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

To access the members of structures we have 2 operators

. (period operator) used to access the members using normal


structure variable

-> (arrow operator) used to access the members using pointer


variable
Structures and pointers
Structures can be created and accessed through pointers

#include<stdio.h>
struct employee
{
char name[20];
int age;
float salary;
};
void main( )
{ e

struct employee e; Name= abc


Age=23
struct employee *p; Salary=25000
p=&e;
printf("Enter employee name\n");
gets(p->name);
Name
printf("Enter employee age\n"); Age
scanf("%d",&p->age); salary

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

ie. A structure which includes another structure is called 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 employee e1;

Now we can access member of date structure as


e1.doj.dd;
e1.doj.mm;
e1.doj.yyyy;
Self referential structure
• Self Referential structures are those structures that have one or more pointers
which point to the same type of structure, as their member.

struct node { n

int data;
struct node* link; data
}; link

int main()
{
struct node n;
return 0;
}
Array of structure

•Example: Store the information of 10 students consisting of name and usn.


The structure definition can be written as shown below:
struct student
{
char name[20] ;
int usn;
};
• struct student s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;

//time consuming
• To store the information of 10 students, the structure variable has to
be declared as follows:
struct student s[10];

The general syntax for structure array declaration is:

struct tag_name arrayname[size];


Write a C program to store and print name,
usn, subject and IA marks of 10 students
using structure {
#include<stdio.h> printf(“Enter student name\n”);
struct student gets(s[i].name);
{ printf(“ Enter usn\n”);
char name[20]; scanf(“%d”, &s[i].usn);
printf(“Enter the subject\n”);
int usn;
gets(s[i].subject);
char subject[20]; printf(“Enter the marks\n”);
float marks; scanf(“%d”, &s[i].marks);
}; }
void main() printf(“students details are\n”);
{ for(i=0;i<10;i++)
struct student s[10]; {
int i; puts(s[i].name);
for(i=0; i<10;i++) printf(“%d”,s[i].usn);
puts(s[i].subject);
printf(“%d”,s[i].marks); } }
sizeof structure
struct student
{
char name[20];
int usn;
}s;
sizeof(s):?
Unions
 Similar to structures
 Collection of data items of similar or dissimilar data types
 All the members of the union share the same memory space
 Only one field of the union is active at any given time

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

You might also like