SlideShare a Scribd company logo
3
Most read
5
Most read
21
Most read
Dr. Rohan Dasgupta
Arrays, String,
Structure and Union
Arrays
● In C programming, an array is a collection of data items
of the same data type, stored at contiguous memory
locations.
● Arrays allow efficient access to a large amount of data,
making them particularly useful for handling collections
of elements.
Arrays - Syntax
● data_type array_name[size];
● Eg. int numbers[5];
● Here, int is the data type, numbers is the array name,
and 5 is the size of the array, indicating that it can store
5 integer elements.
Arrays - Types
● One dimensional Array
● Two dimensional Array
● Multidimensional Array
One Dimensional Arrays
● Stores data in a single row or line.
int arr[5] = {1, 2, 3, 4, 5};
Two Dimensional Arrays
● Often used to represent matrices or tables.
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Multidimensional Arrays
● Allows arrays with multiple dimensions (e.g., 3D, 4D),
though higher dimensions are rarely used due to
complexity.
Arrays - Key Points
● Array indices start at 0.
● Array elements can be accessed by their index, e.g.,
array_name[index].
● Arrays are fixed in size once declared.
Strings
● In C, a string is essentially an array of characters
terminated by a null character ('0').
● The null character at the end indicates the end of the
string.
Strings - Syntax
● char string_name[size];
● Example: char name[10] = "Hello";
● Alternatively, you can initialize strings as follows:
char name[] = "Hello";
● This creates a character array where each character in
"Hello" is stored in consecutive locations, followed by a
'0' at the end.
Strings - Key Functions for Manipulation
● strlen(str): Returns the length of the string (excluding
the null character).
● strcpy(dest, src): Copies the content of src into
dest.
● strcat(dest, src): Appends src at the end of dest.
● strcmp(str1, str2): Compares two strings
Structures
● A structure in C is a user-defined data type that groups
different data types together under a single name.
● It is particularly useful for representing complex data.
Structures - Syntax
struct structure_name
{
data_type1 member1;
data_type2 member2;
...
};
Structures - Example
struct Student {
int id;
char name[50];
float grade;
};
In this example, Student is a structure that has three members:
id (integer), name (character array), and grade (float).
Structures - Key Points
Defining Structure Variables - You can define variables of a
structure type using: struct Student student1;
Accessing Members - Use the dot operator (.) to access structure
members: student1.id = 101;
student1.grade = 85.5;
Nested Structures: Structures can contain other structures as
members, which is useful for more complex data representations.
Union
A union is similar to a structure, but it only allocates memory
for the largest member, and all members share this memory
space.
It is useful when you need a single variable to hold different
types of data at different times.
Union - Syntax
union union_name {
data_type1 member1;
data_type2 member2;
...
};
Union - Example
union Data {
int i;
float f;
char str[20];
};
Union - Key Points
● Only one member can hold a value at a time. If you assign a
value to one member, the previous value stored in any other
member is overwritten.
● Accessing Union Members: Similar to structures, you use the dot
operator.
union Data data;
data.i = 10; // Sets the integer value
data.f = 220.5; // Now `i` is overwritten, and `f` holds a float value
Structure vs. Union
● Memory Usage: In a structure, each member has its own
memory, whereas in a union, all members share the
same memory.
● Usage Scenario: Use structures when you need multiple
data types together. Use unions when you want to store
one of many data types at a time in the same memory
location.
Write short notes on
1. Array
2. String
3. Structure
4. Union
Rubrics:
1. Timely submission: 3 marks (next week), 2 marks (week after)
2. Correct answers: 1.5 marks per question
3. Neatness: 1 mark
Assignment No. 2
Array, String, Structure & Union
Thank you.
Dr. Rohan Dasgupta
C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf

More Related Content

PDF
Introductory manual for the open source potential solver: NEMOH
PDF
DATA STRUCTRES ARRAY AND STRUCTURES CHAPTER 2
PDF
DATA STRUCTURE ARRAY AND STRUCTURES CHAPTER 2
PPTX
17 structure-and-union
PDF
Data Structures Mastery: Sample Paper for Practice"
PPT
Chapter 1 - Introduction to Data Structure.ppt
PPTX
User Defined Datatypes in C++ (Union, enum, class)
PPTX
Array c programming
Introductory manual for the open source potential solver: NEMOH
DATA STRUCTRES ARRAY AND STRUCTURES CHAPTER 2
DATA STRUCTURE ARRAY AND STRUCTURES CHAPTER 2
17 structure-and-union
Data Structures Mastery: Sample Paper for Practice"
Chapter 1 - Introduction to Data Structure.ppt
User Defined Datatypes in C++ (Union, enum, class)
Array c programming

Similar to C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf (20)

PPTX
Introduction to Data Structure
PPTX
Data Structure Introduction for Beginners
PPTX
Introduction linked list
PPTX
DS-UNIT 1 FINAL (2).pptx
PPTX
Data Structures_Introduction
PPTX
"Understanding Arrays in Data Structures: A Beginners Guide."
DOCX
PDF
1. structure
PPTX
Structure in C
PPTX
Array in C
PPTX
DS_PPT.pptx
PPTX
User defined data types.pptx
PDF
Arrays
PPTX
Fundamentals of Structure in C Programming
PPTX
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
PPTX
Data Types - Premetive and Non Premetive
PPTX
Classification of DS.pptx
PDF
Data Structure Ppt for our engineering college industrial training.
PDF
PPT
DS_PPT.ppt
Introduction to Data Structure
Data Structure Introduction for Beginners
Introduction linked list
DS-UNIT 1 FINAL (2).pptx
Data Structures_Introduction
"Understanding Arrays in Data Structures: A Beginners Guide."
1. structure
Structure in C
Array in C
DS_PPT.pptx
User defined data types.pptx
Arrays
Fundamentals of Structure in C Programming
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
Data Types - Premetive and Non Premetive
Classification of DS.pptx
Data Structure Ppt for our engineering college industrial training.
DS_PPT.ppt
Ad

More from Rohan Dasgupta (20)

PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PDF
Green Building Construction - Module 6 - Green Audit and Green Retrofitting
PDF
Green Building Construction - Module 5 - Green Building Rating Systems
PDF
Green Building Construction - Module 4 - Green Building Materials and Indoor ...
PDF
C Programming Lab - Session 1 - Flowcharts for Programs
PDF
Career Opportunities in Civil Engineering
PDF
Green Building Construction - Module 3 - Water Conservation and Energy Effici...
PDF
Green Building Construction - Module 2 - Site Selection Planning and Design
PDF
Green Building Construction - Module 1 - Introduction
PDF
Pitched Roofs
PDF
Town Planning - Rohan Dasgupta.pdf
PDF
Computer Aided Drawing - Rohan Dasgupta.pdf
PDF
How to Create Interactive Videos - Rohan Dasgupta.pdf
PDF
Principles of Planning of School (Public Building)
PDF
How to find out Plagiarism or Similarity Index of Documents using Turnitin - ...
PDF
Pile Foundation - Rohan Dasgupta.pdf
PDF
Openings - Doors and Windows
PDF
Staircase - Introduction, Types, Parts and Design
PPTX
Presentation Zero Template
PDF
Modular Buildings
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
Green Building Construction - Module 6 - Green Audit and Green Retrofitting
Green Building Construction - Module 5 - Green Building Rating Systems
Green Building Construction - Module 4 - Green Building Materials and Indoor ...
C Programming Lab - Session 1 - Flowcharts for Programs
Career Opportunities in Civil Engineering
Green Building Construction - Module 3 - Water Conservation and Energy Effici...
Green Building Construction - Module 2 - Site Selection Planning and Design
Green Building Construction - Module 1 - Introduction
Pitched Roofs
Town Planning - Rohan Dasgupta.pdf
Computer Aided Drawing - Rohan Dasgupta.pdf
How to Create Interactive Videos - Rohan Dasgupta.pdf
Principles of Planning of School (Public Building)
How to find out Plagiarism or Similarity Index of Documents using Turnitin - ...
Pile Foundation - Rohan Dasgupta.pdf
Openings - Doors and Windows
Staircase - Introduction, Types, Parts and Design
Presentation Zero Template
Modular Buildings
Ad

Recently uploaded (20)

PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT
Project quality management in manufacturing
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Sustainable Sites - Green Building Construction
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
composite construction of structures.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Project quality management in manufacturing
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
UNIT-1 - COAL BASED THERMAL POWER PLANTS
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
additive manufacturing of ss316l using mig welding
CH1 Production IntroductoryConcepts.pptx
Foundation to blockchain - A guide to Blockchain Tech
Arduino robotics embedded978-1-4302-3184-4.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Sustainable Sites - Green Building Construction
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
composite construction of structures.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Internet of Things (IOT) - A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx

C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf

  • 1. Dr. Rohan Dasgupta Arrays, String, Structure and Union
  • 2. Arrays ● In C programming, an array is a collection of data items of the same data type, stored at contiguous memory locations. ● Arrays allow efficient access to a large amount of data, making them particularly useful for handling collections of elements.
  • 3. Arrays - Syntax ● data_type array_name[size]; ● Eg. int numbers[5]; ● Here, int is the data type, numbers is the array name, and 5 is the size of the array, indicating that it can store 5 integer elements.
  • 4. Arrays - Types ● One dimensional Array ● Two dimensional Array ● Multidimensional Array
  • 5. One Dimensional Arrays ● Stores data in a single row or line. int arr[5] = {1, 2, 3, 4, 5};
  • 6. Two Dimensional Arrays ● Often used to represent matrices or tables. int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  • 7. Multidimensional Arrays ● Allows arrays with multiple dimensions (e.g., 3D, 4D), though higher dimensions are rarely used due to complexity.
  • 8. Arrays - Key Points ● Array indices start at 0. ● Array elements can be accessed by their index, e.g., array_name[index]. ● Arrays are fixed in size once declared.
  • 9. Strings ● In C, a string is essentially an array of characters terminated by a null character ('0'). ● The null character at the end indicates the end of the string.
  • 10. Strings - Syntax ● char string_name[size]; ● Example: char name[10] = "Hello"; ● Alternatively, you can initialize strings as follows: char name[] = "Hello"; ● This creates a character array where each character in "Hello" is stored in consecutive locations, followed by a '0' at the end.
  • 11. Strings - Key Functions for Manipulation ● strlen(str): Returns the length of the string (excluding the null character). ● strcpy(dest, src): Copies the content of src into dest. ● strcat(dest, src): Appends src at the end of dest. ● strcmp(str1, str2): Compares two strings
  • 12. Structures ● A structure in C is a user-defined data type that groups different data types together under a single name. ● It is particularly useful for representing complex data.
  • 13. Structures - Syntax struct structure_name { data_type1 member1; data_type2 member2; ... };
  • 14. Structures - Example struct Student { int id; char name[50]; float grade; }; In this example, Student is a structure that has three members: id (integer), name (character array), and grade (float).
  • 15. Structures - Key Points Defining Structure Variables - You can define variables of a structure type using: struct Student student1; Accessing Members - Use the dot operator (.) to access structure members: student1.id = 101; student1.grade = 85.5; Nested Structures: Structures can contain other structures as members, which is useful for more complex data representations.
  • 16. Union A union is similar to a structure, but it only allocates memory for the largest member, and all members share this memory space. It is useful when you need a single variable to hold different types of data at different times.
  • 17. Union - Syntax union union_name { data_type1 member1; data_type2 member2; ... };
  • 18. Union - Example union Data { int i; float f; char str[20]; };
  • 19. Union - Key Points ● Only one member can hold a value at a time. If you assign a value to one member, the previous value stored in any other member is overwritten. ● Accessing Union Members: Similar to structures, you use the dot operator. union Data data; data.i = 10; // Sets the integer value data.f = 220.5; // Now `i` is overwritten, and `f` holds a float value
  • 20. Structure vs. Union ● Memory Usage: In a structure, each member has its own memory, whereas in a union, all members share the same memory. ● Usage Scenario: Use structures when you need multiple data types together. Use unions when you want to store one of many data types at a time in the same memory location.
  • 21. Write short notes on 1. Array 2. String 3. Structure 4. Union Rubrics: 1. Timely submission: 3 marks (next week), 2 marks (week after) 2. Correct answers: 1.5 marks per question 3. Neatness: 1 mark Assignment No. 2 Array, String, Structure & Union