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

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

  • 1.
    Dr. Rohan Dasgupta Arrays,String, Structure and Union
  • 2.
    Arrays ● In Cprogramming, 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 ● Allowsarrays with multiple dimensions (e.g., 3D, 4D), though higher dimensions are rarely used due to complexity.
  • 8.
    Arrays - KeyPoints ● 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 - KeyFunctions 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 structurein 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 structstructure_name { data_type1 member1; data_type2 member2; ... };
  • 14.
    Structures - Example structStudent { 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 - KeyPoints 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 issimilar 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 unionunion_name { data_type1 member1; data_type2 member2; ... };
  • 18.
    Union - Example unionData { int i; float f; char str[20]; };
  • 19.
    Union - KeyPoints ● 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 noteson 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
  • 22.