ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CSI-311
    Progress0 / 17 topics
    Topics
    1. Overview of Computers and Programming2. Overview of Languages (e.g., C Language)3. Basics of Structured and Modular Programming4. Basic Algorithms and Problem Solving5. Development of Basic Algorithms6. Analyzing Problems7. Designing Solutions8. Testing Designed Solutions9. Fundamental Programming Constructs10. Translation of Algorithms to Programs11. Data Types12. Control Structures13. Functions14. Arrays15. Records16. Files17. Testing Programs
    CSI-311›Arrays
    Programming FundamentalsTopic 14 of 17

    Arrays

    7 minread
    1,218words
    Intermediatelevel

    Arrays in C Language

    An array is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays allow you to store multiple values under a single variable name, which makes it easier to work with large amounts of related data. In C, arrays are one of the most fundamental data structures and are commonly used for handling collections of data, such as lists of numbers, strings, or other types of values.


    1. Declaring an Array in C

    To declare an array in C, you specify the type of the elements followed by the array's name and the number of elements it will hold, enclosed in square brackets.

    • Syntax:

      type array_name[array_size];
      
    • Example:

      int numbers[5];  // Declares an integer array of size 5
      

    Here, numbers is an array of integers with space for 5 elements. The index positions for the array elements will be from 0 to 4.


    2. Initializing Arrays in C

    You can initialize an array at the time of declaration or later in your program. In C, arrays are indexed starting from 0.

    a) Initializing at the time of declaration

    You can initialize an array with values directly when you declare it. The number of values you specify determines the size of the array.

    • Syntax:

      type array_name[] = {value1, value2, value3, ..., valueN};
      
    • Example:

      int numbers[] = {10, 20, 30, 40, 50};  // An array of 5 integers
      

    If the array size is not explicitly specified, C will determine the size based on the number of values in the initializer list (in this case, 5).

    b) Partial Initialization

    If you don't initialize all elements, the remaining elements will automatically be initialized to zero (for numeric types).

    • Example:
      int numbers[5] = {10, 20};  // Only first two elements initialized, others default to 0
      
      Here, numbers[0] = 10, numbers[1] = 20, and numbers[2], numbers[3], and numbers[4] will be 0.

    3. Accessing Array Elements

    Array elements are accessed using their index, starting from 0. You specify the index inside square brackets to access or modify a particular element.

    • Syntax:

      array_name[index];
      
    • Example:

      int numbers[] = {10, 20, 30, 40, 50};
      printf("%d", numbers[2]);  // Output: 30 (accesses the 3rd element)
      numbers[3] = 100;          // Modifies the 4th element to 100
      

    In the example above, numbers[2] refers to the element at index 2 (which is 30), and numbers[3] = 100 updates the element at index 3 to 100.


    4. Multi-Dimensional Arrays

    C supports multi-dimensional arrays, which are arrays of arrays. The most common example is a two-dimensional array, which can be visualized as a table with rows and columns.

    a) Declaring a Two-Dimensional Array

    A two-dimensional array is declared by specifying the number of rows and columns.

    • Syntax:

      type array_name[rows][columns];
      
    • Example:

      int matrix[3][3];  // A 3x3 matrix (3 rows and 3 columns)
      

    b) Initializing a Two-Dimensional Array

    You can initialize a 2D array in a similar way to a 1D array, using curly braces for each row.

    • Syntax:

      type array_name[rows][columns] = {{value1, value2, value3}, {value4, value5, value6}, ...};
      
    • Example:

      int matrix[2][2] = {{1, 2}, {3, 4}};  // A 2x2 matrix
      

    c) Accessing Elements in a 2D Array

    You access elements by specifying both the row and column index.

    • Syntax:

      array_name[row_index][column_index];
      
    • Example:

      int matrix[2][2] = {{1, 2}, {3, 4}};
      printf("%d", matrix[1][1]);  // Output: 4 (accesses the element in 2nd row and 2nd column)
      

    5. Arrays of Strings

    In C, an array of strings is an array of character arrays. Each string is represented as an array of characters, with the last character being a null character (\0).

    • Declaring an Array of Strings:

      char strings[3][10];  // An array of 3 strings, each of maximum length 9 (1 for the null terminator)
      
    • Initializing an Array of Strings:

      char strings[3][10] = {"Hello", "World", "C"};
      
    • Accessing Elements:

      printf("%s", strings[0]);  // Output: Hello
      

    Each string is stored as a character array, and strings[0] refers to the first string, strings[1] refers to the second, and so on.


    6. Array Size

    In C, the size of an array is fixed at the time of declaration and cannot be changed during the execution of the program. However, you can determine the number of elements in a statically allocated array by using the sizeof operator.

    • Syntax:

      int size = sizeof(array_name) / sizeof(array_name[0]);
      
    • Example:

      int numbers[] = {10, 20, 30, 40, 50};
      int size = sizeof(numbers) / sizeof(numbers[0]);  // size will be 5
      

    The sizeof(numbers) gives the total size in bytes of the entire array, and sizeof(numbers[0]) gives the size of a single element in the array. Dividing these gives the total number of elements.


    7. Limitations of Arrays in C

    1. Fixed Size: Once an array is created, its size cannot be changed dynamically. This makes arrays less flexible in certain situations.

    2. Memory Allocation: Arrays are allocated in contiguous memory locations, so large arrays may require a significant amount of memory, and inefficient memory usage may result in problems.

    3. No Bounds Checking: C does not perform bounds checking, so accessing an array element outside its declared size can lead to undefined behavior or memory corruption.


    8. Array Operations

    a) Traversing an Array

    You can traverse an array using a loop to access and process each element.

    • Example:
      int numbers[] = {10, 20, 30, 40, 50};
      for (int i = 0; i < 5; i++) {
          printf("%d ", numbers[i]);  // Output: 10 20 30 40 50
      }
      

    b) Searching an Element

    To search for an element in an array, you can use a loop to check each element.

    • Example:
      int numbers[] = {10, 20, 30, 40, 50};
      int target = 30;
      int found = 0;
      for (int i = 0; i < 5; i++) {
          if (numbers[i] == target) {
              found = 1;
              break;
          }
      }
      if (found) {
          printf("Element found\n");
      } else {
          printf("Element not found\n");
      }
      

    Summary of Arrays in C

    • Definition: Arrays are collections of elements of the same data type stored in contiguous memory locations.
    • Declaring Arrays: You declare arrays by specifying the type and size.
    • Accessing Elements: You access elements using the index (starting from 0).
    • Multi-Dimensional Arrays: You can work with two-dimensional or higher-dimensional arrays.
    • String Arrays: Arrays of strings are arrays of character arrays.
    • Limitations: Arrays have fixed sizes, and C does not provide bounds checking, so you must ensure you do not access out-of-bounds elements.

    Arrays are a fundamental concept in C programming, allowing you to handle multiple pieces of related data efficiently.

    Previous topic 13
    Functions
    Next topic 15
    Records

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,218
      Code examples0
      DifficultyIntermediate