You are on page 1of 11

Introduction to

Structures in C
CSC-103 Programming Fundamentals
CSC-141 Introduction to Computer Programming

Slides prepared by: Dr. Omar Ahmad


Lecture outline
• What are structures?
• Why do we need structures?
• Declaring a structure
• Defining structure variables
• Memory allocation for structures
• Accessing structure members
• Structure initialization
• Structures as function arguments

2
What are structures?
• Structure is a user defined data type available in C that allows to
combine data items of different kinds.
• Structures are often used to represent a record.
• Example: Record for a company employee with following attributes

• Employee ID (int in C)
• Name (char array in C)
• Salary (float in C)

3
Why do we need structures?
• Arrays are one possible
alternative to using
structures.
• Construct individual arrays for
storing Names, IDs, and
Salary.
• Cumbersome to build
• Difficult to access record of
one employee
• Structures are much more
efficient
4
Declaring a structure
• Use the struct keyword struct employee
• Declares a new data type, with more than one { int id;
member. char name[20];
• The structure tag is optional float salary;
• Each member definition is a normal variable };
definition
• The closing brace in the structure type
declaration must be followed by a ;
• Structures are usually declared at the beginning
of the programs (outside any function)
• Memory is NOT allocated

5
Defining structure variables
struct employee
• Memory will be allocated for
{ int id;
structure variables only.
char name[20];
• Two different ways to define float salary;
structure variables };
struct employee emp1,
emp2;
struct employee
struct //tag is optional
{ int id;
{ int id;
char name[20];
char name[20];
float salary;
float salary;
} emp1, emp2 ;
}emp1, emp2; 6
Memory allocation for structures

https://www.javatpoint.com/structure-in-c
7
Accessing structure members
• Members of a structure are struct employee
accessed using the dot (.) { int id;
operator. char name[20];
• Can be used with scanf() and float salary;
printf()functions. } emp1, emp2 ;
• They can also be accessed using
emp1.id = 45; // Assign value
the arrow -> operator when using
pointers (discussed later) to a member
emp2.id = 48;
int x = emp1.id; // Read
member value
8
Structure initialization
struct Patient
• A structure variable may {
float height;
be initialized in one int weight;
int age;
command using curly };

brackets.  
struct Patient p1 = { 180.75 , 73, 23 }; //initialization
 
• Individual members of a
structure variable may struct Patient p1;
p1.height = 180.75; //initialization of each member separately
be initialized separately p1.weight = 73;
p1.age = 23;

• Members may not be  


struct Point
initialized when {

structure is declared
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};

9
Passing structures as function arguments
void print_student_val(struct student record);
void print_student_ref(struct student *record);
• A structure may be passed int main()
{
as a function argument in struct student record;
two ways  
record.id=1;
• Pass by value strcpy(record.name, “Tanveer");
record.percentage = 86.5;
• Pass by reference (address) print_student_val(record);

• Name of the structure print_student_ref(&record);


return 0;
variable is used to pass the }
 
structure by value void print_student_ref(struct student *record){
printf(" Id is: %d \n", record->id);
void print_student_val(struct student record) printf(" Name is: %s \n", record->name);
{ printf(" Percentage is: %f \n", record-
printf(" Id is: %d \n", record.id); >percentage);
printf(" Name is: %s \n", record.name); }
printf(" Percentage is: %f \n", record.percentage);
} 10
References
• https://www.tutorialspoint.com/cprogramming/c_structures.htm
• https://www.javatpoint.com/structure-in-c
• https://www.studytonight.com/c/structures-in-c.php
• https://www.geeksforgeeks.org/structures-c/
• https://beginnersbook.com/2014/01/c-structures-examples/

11

You might also like