Chapter 2 Structure Part 1
Chapter 2 Structure Part 1
Code:20CST111
Unit-3
• For example: If I have to write a program to store Student information, which will
have Student's name, age, branch, permanent address, father's name etc, which
included string values, integer values etc, how can I use arrays for this problem, I
will require something which can hold data of different types together. In structure,
data is stored in form of records.
5
Defining a Structure
• struct keyword is used to define a structure. struct defines a new data type which is a collection
of primary and derived datatypes.
• Syntax:
struct [structure_tag]
{
//member variable 1
//member variable 2
...
}[structure_variables];
Note: The closing curly brace in the structure type declaration must be followed by a semicolon(;).
6
Example:
struct Student
{
char name[25];
int age;
char branch[10];
// F for female and M for male
char gender;
};
Here struct Student declares a structure to hold the details of a student which consists of 4
data fields, namely name, age, branch and gender. These fields are called structure
elements or members.
Each member can have different datatype, like in this case, name is an array of char type
and age is of int type etc. Student is the name of the structure and is called as the structure
tag.
7
Declaring Structure Variables
• It is possible to declare variables of a structure, either along with
structure definition or after the structure is defined.
9
Declaring Structure variables with structure
definition
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
}S1, S2;
Here S1 and S2 are variables of structure Student.
10
Initializing structure members
• Structure members cannot be initialized with declaration. For example the
following C program fails in compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
11
12
Structure members can be initialized using curly braces ‘{}’. For
example, following is a valid initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
13
Accessing Structure Members
• Structure members can be accessed and assigned values in a number
of ways.
• Structure members have no meaning individually without the
structure.
• There are two types of operators used for accessing members of a
structure.
1. . Member operator
2. -> -Structure pointer operator
14
Accessing Structure Members by Dot Operator
Example:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
return 0;
}
Output:
x = 20, y = 1
15
Accessing Structure Members by Structure
pointer operator
Example:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
Output:
12
16
17
18
Union
Like Structures, union is a user defined data type.
19
20
21
• In union, all members share the same memory location
22
For example, In the following C program, both x and y share the same location. If we change x, we
can see the changes being reflected in y.
#include <stdio.h>
// Declaration of union is same as structures
union test {
int x, y;
};
int main()
{
// A union variable t
union test t;
t.x = 2; // t.y also gets value 2
printf("After making x = 2:\n x = %d, y = %d\n\n",
t.x, t.y);
23
Output:
After making x = 2:
x = 2, y = 2
24
Structure Vs Union
25
Uses of C Structure
• C Structures can be used to store huge data. Structures act as a
database.
• C Structures can be used to send data to the printer.
• C Structures can interact with keyboard and mouse to store the data.
• C Structures can be used in drawing and floppy formatting.
• C Structures can be used to clear output screen contents.
• C Structures can be used to check computer’s memory size etc.
26
Summary
27
Frequently Asked question
Q1 What will be the output of the C program?
#include<stdio.h>
int main()
{
struct leader
{
char *lead;
int born;
};
struct leader l1 = {"AbdulKalam", 1931};
struct leader l2 = l1;
printf("%s %d", l2.lead, l1.born);
28
Q2. What will be the output of the C program?
void main()
{
struct bitfields {
int bits_1: 2;
int bits_2: 9;
int bits_3: 6;
int bits_4: 1;
}bit;
printf("%d", sizeof(bit));
}
Ans: 3
29
Q4. Write a program to Store Information in Structure and Display it?
#include <stdio.h> scanf("%s", s[i].firstName);
struct student { printf("Enter marks: ");
char firstName[50]; scanf("%f", &s[i].marks);
int roll; }
float marks;
} s[10]; printf("Displaying Information:\n\n");
30
Output:
31
Assessment Questions:
1. What is the output?
#include <stdio.h>
int main()
{
struct xyz{
int a;
};
#include<stdio.h>
int main()
{
struct simp
{
int i = 6;
char city[] = "chennai";
};
struct simp s1;
printf("%d",s1.city);
printf("%d", s1.i);
return 0;
33
4. What will be the output of the program?
#include <stdio.h>
struct cppbuzz{
};
int main()
{
printf("%d",sizeof(struct cppbuzz));
return 0;
}
34
Discussion forum.
• Watch this video to know more about how structures are stored in
memory and what is meant by structure padding.
https://www.youtube.com/watch?v=aROgtACPjjg
35
REFERENCES
Reference Books
1. Programming in C by Reema Thareja.
2. Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
3. Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
4. The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.
Websites:
1. https://www.geeksforgeeks.org/structures-c/
2. https://www.studytonight.com/c/structures-in-c.php
3. https://www.tutorialspoint.com/cprogramming/c_structures.htm
4. https://www.geeksforgeeks.org/union-c/
YouTube Links:
1. https://www.youtube.com/watch?v=Ranc3VvjI88
2. https://www.youtube.com/watch?v=zdUhS4YSWHg
3. https://www.youtube.com/watch?v=t7MD-Elr05k
36
THANK YOU
For queries
Email: [email protected]