Embed presentation
Download to read offline

![Programming in C Unit-III BCA Semester I2Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDefinition of an Array• An array is a finite, ordered, homogeneous collection of elements stored in contiguousmemory locations, accessed using a common name and an index.• It is a linear static data structure, meaning its size is fixed during declaration.Examples of Arrays1.Integer Arrayint numArray[12] = {11, 9, 17, 89, 1, 90, 19, 5, 3, 23, 43, 99};2. Float Arrayfloat marks[5] = {88.5, 76.0, 92.3, 69.5, 80.0};3.Character Array (String)char subject[8] = "Computer";Characteristics of Arrays1. Arrays store multiple elements of the same data type.2. All elements are placed in contiguous (continuous) memory locations.3. Each element is accessed using an index, with C following 0-based indexing.4. The size of an array is fixed at the time of declaration and cannot be changed later.5. Arrays provide fast and direct access to elements through indexing.Need for Arrays1. Arrays store many values of the same data type efficiently.2. They remove the need to declare multiple separate variables.3. Arrays provide quick access to elements using indices.4. They make programs simpler and easier to manage.5. Arrays help in systematic processing of large data sets.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-2-2048.jpg&f=jpg&w=240)

![Programming in C Unit-III BCA Semester I4Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaClassification of ArraysIn C programming, arrays are classified mainly based on their dimensionality. There are two typesof arrays:I) One Dimensional arrays (1-D arrays)DefinitionA one-dimensional array is a collection of elements of the same data type arranged in a singlerow (linear form) and stored in contiguous memory locations.a)1-D Array Declaration in C• To declare an array in C, we use square brackets [] after the array name.• The size of the array (number of elements) is written inside the brackets.Syntax:data_type array_name[size];➢ data_type → type of elements (int, float, char, etc.)➢ array_name → name of the array➢ size → number of elementsExamples:int numbers[10]; // array of 10 integersfloat marks[5]; // array of 5 floatschar name[20]; // array of 20 characters (strings)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-4-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I5Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcab) Types of 1-D Array Initialization in C1. Compile-time (static)InitializationValues are assigned at the time of declaration.int marks[5] = {90, 85, 78, 92, 88};2. Partial InitializationOnly some values are given; the remaining are set to 0.int numbers[5] = {1, 2}; // stores 1, 2, 0, 0, 03. Implicit Size InitializationThe size is automatically determined by the number of elements.int nums[] = {10, 20, 30, 40};4. Runtime (Dynamic) InitializationValues are entered by the user or assigned during program execution.int arr[5];for(int i = 0; i < 5; i++){scanf("%d", &arr[i]);}c) Accessing Elements of a 1-D ArrayUse the index (starting from 0) to read an element:#include <stdio.h>int main(){int arr[5] = {10, 20, 30, 40, 50}; // Compile-time initialization// Access elementsprintf("First element: %dn", arr[0]); //10printf("Third element: %dn", arr[2]); //30return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-5-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I6Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcad) Updating Elements of a 1-D ArrayAssign a new value to an element using its index:#include <stdio.h>int main(){int arr[5] = {10, 20, 30, 40, 50};// Update elementsarr[1] = 25; // Update second elementarr[4] = 55; // Update fifth element// Display updated arrayprintf("nUpdated Array Elements:n");for(int i = 0; i < 5; i++){printf("arr[%d] = %dn", i, arr[i]);}return 0;}e) Memory Representation of 1-D Array• In C, the elements of an array are stored in continuous memory locations.• Each element is accessed using an index, which starts from 0.• The base address refers to the memory location of the first element.• The address of the i-th element is calculated as:Address of element = Base address + (i × size of each element)Example:• If int marks[5] = {90, 97, 95, 99, 100}; is stored at base address 3020 and each integertakes 2 bytes (depends on system), memory allocation will be:](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-6-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I7Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaII) Multidimensional Arrays• In C, arrays store multiple values of the same data type.• Multidimensional arrays represent data in tables, grids, or matrices (e.g., student marks,chessboard, image pixels).• A multidimensional array is an array of arrays, created with more than one dimension.• Common types include 2-D, 3-D, and higher-dimensional arrays, with 2-D arrays being themost common (rows and columns).Syntax for declaration:data_type array_name[size1][size2]........[sizeN];Examples:int matrix[3][3]; // 2-D array (3 rows, 3 columns)int cube[3][3][3]; // 3-D array (like a cube of data)Two-Dimensional (2-D) ArraysDefinitionA two-dimensional (2-D) array is a collection of elements arranged in rows and columns,where each element is accessed using two indices.• It is used to store data in the form of a table or matrix with rows and columns.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-7-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I8Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaa) Declaration of 2-D ArraysTo declare a 2-D array, we use two sets of square brackets [ ] [ ]:Syntax:data_type array_name[rows][columns];• data_type → type of elements (int, float, char, etc.)• array_name → name of the array• rows, columns → size (number of rows and columns)Example:int matrix[3][3]; // 3x3 integer matrix, this can store 3 × 3 = 09 elements.float marks[2][5]; // 2 rows and 5 columns of floats, this can store 2 × 5 = 10 elements.char name[4][20]; // 4 strings, each of max length 20, his can store 4 × 20 = 80 elements.b) Types of 2-D Array Initialization1. Compile-time InitializationValues assigned during declaration.int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };2. Partial InitializationRemaining elements are set to 0.int mat[2][3] = { {1, 2}, {4} }; // stores: {1,2,0} and {4,0,0}3. Implicit Row Size InitializationNumber of rows can be left empty (columns must be given).int mat[][3] = { {1, 2, 3}, {4, 5, 6} };](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-8-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I9Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca4. Runtime (Dynamic) InitializationValues entered by user at runtime.int mat[2][3];for(int i = 0; i < 2; i++){for(int j = 0; j < 3; j++){scanf("%d", &mat[i][j]);}}c) Accessing Elements of a 2-D ArrayUse row index and column index.#include <stdio.h>int main(){int mat[2][3] = { {10, 20, 30}, {40, 50, 60} };printf("Element at [0][1] = %dn", mat[0][1]); // 20printf("Element at [1][2] = %dn", mat[1][2]); // 60return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-9-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I10Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcad) Updating Elements of a 2-D ArrayAssign new values using row and column index.#include <stdio.h>int main(){int mat[2][3] = { {10, 20, 30}, {40, 50, 60} };// Update elementsmat[0][1] = 25; // Update row 0, col 1mat[1][2] = 65; // Update row 1, col 2// Display updated matrixprintf("nUpdated Matrix:n");for(int i = 0; i < 2; i++){for(int j = 0; j < 3; j++){printf("%d ", mat[i][j]);}printf("n");}return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-10-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I11Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcae) Memory representation of 2-D Arrayint arr[3][3] = { {24, 15, 34}, {26, 134, 194}, {67, 23, 345} } is stored in memory asHere, the base address is the address of the 2-D array or the address of the first object stored in the2-D array. The memory address increases by 4 bytes (the size of the int data type).II) StructuresIntroduction:• A structure in C is a user-defined data type that groups variables of different types undera single name.• It is useful for representing real-world entities, such as student details or textbookinformation.Definition:A structure is a collection of heterogeneous variables under a single name, enabling logicalrepresentation of real-world entities and improving program organisation.Syntax:• A structure is defined using the keyword struct.• The items in the structure are called its members and they can be of any valid data type.struct StructureName{data_type member1;data_type member2;...data_type memberN;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-11-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I12Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaExample:1Memory Size of a Structure:• The total memory size of a structure is the sum of the sizes of its members plus anypadding added by the compiler for alignment.Example:2struct textbook{char name[40];char author[20];int pages;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-12-2048.jpg&f=jpg&w=240)

![Programming in C Unit-III BCA Semester I14Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDeclaring Structure VariablesStructure variables can be declared in two ways:1)Method 1 – After structure definition:struct Student{int rollno;char name[20];float marks;};struct Student s1, s2; // declaring variables2)Method 2 – Along with structure definition:struct Student{int rollno;char name[20];float marks;} s1, s2;Initializing Structure Members1) Initialize a structure at the time of declaration:#include <stdio.h>// Define the structurestruct Student{int rollno;char name[20];float marks;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-14-2048.jpg&f=jpg&w=240)


![Programming in C Unit-III BCA Semester I17Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDisadvantages of Unions1. Only one member can hold a value at a time.2. Assigning a value to one member overwrites the previous value.3. Cannot store multiple values simultaneously.4. Accessing the wrong member may lead to unexpected results.5. Not suitable when multiple data elements need to be stored concurrently.Declaration of UnionUnions are declared using the keyword union.Syntax:union UnionName{data_type member1;data_type member2;...data_type memberN;};Initialization of Union1)Method 1 – At the time of declaration:#include <stdio.h>// Define the unionunion Data{int i;float f;char str[20];};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-17-2048.jpg&f=jpg&w=240)



Unit-3Arrays - One Dimensional arrays - Declaration, Initialization and Memoryrepresentation. Two Dimensional arrays - Declaration, Initialization and Memoryrepresentation.Structures - Structure Definition, Advantages of Structure, declaring , initializationaccessing structure members. Unions - Union definition,declaration,initialization.difference between Structures and Unions

![Programming in C Unit-III BCA Semester I2Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDefinition of an Array• An array is a finite, ordered, homogeneous collection of elements stored in contiguousmemory locations, accessed using a common name and an index.• It is a linear static data structure, meaning its size is fixed during declaration.Examples of Arrays1.Integer Arrayint numArray[12] = {11, 9, 17, 89, 1, 90, 19, 5, 3, 23, 43, 99};2. Float Arrayfloat marks[5] = {88.5, 76.0, 92.3, 69.5, 80.0};3.Character Array (String)char subject[8] = "Computer";Characteristics of Arrays1. Arrays store multiple elements of the same data type.2. All elements are placed in contiguous (continuous) memory locations.3. Each element is accessed using an index, with C following 0-based indexing.4. The size of an array is fixed at the time of declaration and cannot be changed later.5. Arrays provide fast and direct access to elements through indexing.Need for Arrays1. Arrays store many values of the same data type efficiently.2. They remove the need to declare multiple separate variables.3. Arrays provide quick access to elements using indices.4. They make programs simpler and easier to manage.5. Arrays help in systematic processing of large data sets.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-2-2048.jpg&f=jpg&w=240)

![Programming in C Unit-III BCA Semester I4Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaClassification of ArraysIn C programming, arrays are classified mainly based on their dimensionality. There are two typesof arrays:I) One Dimensional arrays (1-D arrays)DefinitionA one-dimensional array is a collection of elements of the same data type arranged in a singlerow (linear form) and stored in contiguous memory locations.a)1-D Array Declaration in C• To declare an array in C, we use square brackets [] after the array name.• The size of the array (number of elements) is written inside the brackets.Syntax:data_type array_name[size];➢ data_type → type of elements (int, float, char, etc.)➢ array_name → name of the array➢ size → number of elementsExamples:int numbers[10]; // array of 10 integersfloat marks[5]; // array of 5 floatschar name[20]; // array of 20 characters (strings)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-4-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I5Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcab) Types of 1-D Array Initialization in C1. Compile-time (static)InitializationValues are assigned at the time of declaration.int marks[5] = {90, 85, 78, 92, 88};2. Partial InitializationOnly some values are given; the remaining are set to 0.int numbers[5] = {1, 2}; // stores 1, 2, 0, 0, 03. Implicit Size InitializationThe size is automatically determined by the number of elements.int nums[] = {10, 20, 30, 40};4. Runtime (Dynamic) InitializationValues are entered by the user or assigned during program execution.int arr[5];for(int i = 0; i < 5; i++){scanf("%d", &arr[i]);}c) Accessing Elements of a 1-D ArrayUse the index (starting from 0) to read an element:#include <stdio.h>int main(){int arr[5] = {10, 20, 30, 40, 50}; // Compile-time initialization// Access elementsprintf("First element: %dn", arr[0]); //10printf("Third element: %dn", arr[2]); //30return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-5-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I6Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcad) Updating Elements of a 1-D ArrayAssign a new value to an element using its index:#include <stdio.h>int main(){int arr[5] = {10, 20, 30, 40, 50};// Update elementsarr[1] = 25; // Update second elementarr[4] = 55; // Update fifth element// Display updated arrayprintf("nUpdated Array Elements:n");for(int i = 0; i < 5; i++){printf("arr[%d] = %dn", i, arr[i]);}return 0;}e) Memory Representation of 1-D Array• In C, the elements of an array are stored in continuous memory locations.• Each element is accessed using an index, which starts from 0.• The base address refers to the memory location of the first element.• The address of the i-th element is calculated as:Address of element = Base address + (i × size of each element)Example:• If int marks[5] = {90, 97, 95, 99, 100}; is stored at base address 3020 and each integertakes 2 bytes (depends on system), memory allocation will be:](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-6-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I7Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaII) Multidimensional Arrays• In C, arrays store multiple values of the same data type.• Multidimensional arrays represent data in tables, grids, or matrices (e.g., student marks,chessboard, image pixels).• A multidimensional array is an array of arrays, created with more than one dimension.• Common types include 2-D, 3-D, and higher-dimensional arrays, with 2-D arrays being themost common (rows and columns).Syntax for declaration:data_type array_name[size1][size2]........[sizeN];Examples:int matrix[3][3]; // 2-D array (3 rows, 3 columns)int cube[3][3][3]; // 3-D array (like a cube of data)Two-Dimensional (2-D) ArraysDefinitionA two-dimensional (2-D) array is a collection of elements arranged in rows and columns,where each element is accessed using two indices.• It is used to store data in the form of a table or matrix with rows and columns.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-7-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I8Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaa) Declaration of 2-D ArraysTo declare a 2-D array, we use two sets of square brackets [ ] [ ]:Syntax:data_type array_name[rows][columns];• data_type → type of elements (int, float, char, etc.)• array_name → name of the array• rows, columns → size (number of rows and columns)Example:int matrix[3][3]; // 3x3 integer matrix, this can store 3 × 3 = 09 elements.float marks[2][5]; // 2 rows and 5 columns of floats, this can store 2 × 5 = 10 elements.char name[4][20]; // 4 strings, each of max length 20, his can store 4 × 20 = 80 elements.b) Types of 2-D Array Initialization1. Compile-time InitializationValues assigned during declaration.int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };2. Partial InitializationRemaining elements are set to 0.int mat[2][3] = { {1, 2}, {4} }; // stores: {1,2,0} and {4,0,0}3. Implicit Row Size InitializationNumber of rows can be left empty (columns must be given).int mat[][3] = { {1, 2, 3}, {4, 5, 6} };](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-8-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I9Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bca4. Runtime (Dynamic) InitializationValues entered by user at runtime.int mat[2][3];for(int i = 0; i < 2; i++){for(int j = 0; j < 3; j++){scanf("%d", &mat[i][j]);}}c) Accessing Elements of a 2-D ArrayUse row index and column index.#include <stdio.h>int main(){int mat[2][3] = { {10, 20, 30}, {40, 50, 60} };printf("Element at [0][1] = %dn", mat[0][1]); // 20printf("Element at [1][2] = %dn", mat[1][2]); // 60return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-9-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I10Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcad) Updating Elements of a 2-D ArrayAssign new values using row and column index.#include <stdio.h>int main(){int mat[2][3] = { {10, 20, 30}, {40, 50, 60} };// Update elementsmat[0][1] = 25; // Update row 0, col 1mat[1][2] = 65; // Update row 1, col 2// Display updated matrixprintf("nUpdated Matrix:n");for(int i = 0; i < 2; i++){for(int j = 0; j < 3; j++){printf("%d ", mat[i][j]);}printf("n");}return 0;}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-10-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I11Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcae) Memory representation of 2-D Arrayint arr[3][3] = { {24, 15, 34}, {26, 134, 194}, {67, 23, 345} } is stored in memory asHere, the base address is the address of the 2-D array or the address of the first object stored in the2-D array. The memory address increases by 4 bytes (the size of the int data type).II) StructuresIntroduction:• A structure in C is a user-defined data type that groups variables of different types undera single name.• It is useful for representing real-world entities, such as student details or textbookinformation.Definition:A structure is a collection of heterogeneous variables under a single name, enabling logicalrepresentation of real-world entities and improving program organisation.Syntax:• A structure is defined using the keyword struct.• The items in the structure are called its members and they can be of any valid data type.struct StructureName{data_type member1;data_type member2;...data_type memberN;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-11-2048.jpg&f=jpg&w=240)
![Programming in C Unit-III BCA Semester I12Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaExample:1Memory Size of a Structure:• The total memory size of a structure is the sum of the sizes of its members plus anypadding added by the compiler for alignment.Example:2struct textbook{char name[40];char author[20];int pages;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-12-2048.jpg&f=jpg&w=240)

![Programming in C Unit-III BCA Semester I14Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDeclaring Structure VariablesStructure variables can be declared in two ways:1)Method 1 – After structure definition:struct Student{int rollno;char name[20];float marks;};struct Student s1, s2; // declaring variables2)Method 2 – Along with structure definition:struct Student{int rollno;char name[20];float marks;} s1, s2;Initializing Structure Members1) Initialize a structure at the time of declaration:#include <stdio.h>// Define the structurestruct Student{int rollno;char name[20];float marks;};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-14-2048.jpg&f=jpg&w=240)


![Programming in C Unit-III BCA Semester I17Notes by Dr. Chandrakantha T S, Vagdevi College of BCA, Melinakuruvalli, Thirthahalli-577 432📚 For more notes and resources, visit: https://sites.google.com/view/chandrakanthats/bcaDisadvantages of Unions1. Only one member can hold a value at a time.2. Assigning a value to one member overwrites the previous value.3. Cannot store multiple values simultaneously.4. Accessing the wrong member may lead to unexpected results.5. Not suitable when multiple data elements need to be stored concurrently.Declaration of UnionUnions are declared using the keyword union.Syntax:union UnionName{data_type member1;data_type member2;...data_type memberN;};Initialization of Union1)Method 1 – At the time of declaration:#include <stdio.h>// Define the unionunion Data{int i;float f;char str[20];};](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fcunit-iii-251203071832-8fd0f134%2f75%2fProgramming-in-C-Unit-III-Arrays-Structures-and-Unions-17-2048.jpg&f=jpg&w=240)

