C Programming Lab - Session 7 - Arrays, String, Structure and Union.pdf
The document outlines key concepts of arrays, strings, structures, and unions in C programming. It explains the syntax, types, and key points for each data structure, emphasizing their uses and characteristics. Additionally, it includes a rubric for assignment evaluation related to these topics.
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.
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.
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.
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