Home/Notes/Arrays & Strings
Board Exam Notes

Arrays & Strings Notes

Questions

3 questions per semester paper

Difficulty

Medium

Importance

Core curriculum high-yield topic

Overview

Arrays and strings form the foundation of data storage and manipulation in C/C++, serving as the primary contiguous memory structures for university curriculum. Mastering these topics is essential as they frequently appear in semester exams, often linked with pointer arithmetic and string handling library functions. Aspirants must grasp the concept of index-based access and memory layout to solve implementation-based problems effectively.

1D and 2D Arrays

Arrays are collection of elements of the same data type stored in contiguous memory locations. 1D arrays are linear, while 2D arrays are organized as matrices requiring row-major or column-major indexing.

  • 1D Array Syntax: data_type array_name[size]
  • Indexing starts from 0 to size-1
  • 2D Array Syntax: data_type array_name[rows][cols]
  • Row-major order stores consecutive rows in memory
  • Memory formula for 2D: Address[i][j] = Base + (i * TotalCols + j) * size

String Handling Functions

Strings in C are treated as character arrays terminated by a null character '\0'. Standard library functions in <string.h> simplify manipulation tasks like copying, concatenating, and comparing.

  • strlen(): Returns the length of a string excluding '\0'
  • strcpy(dest, src): Copies source string to destination
  • strcat(dest, src): Concatenates source to destination
  • strcmp(s1, s2): Returns 0 if strings are equal
  • All strings must end with null character '\0'

Array-Pointer Relationship

In C, the array name acts as a constant pointer to the first element of the array. This duality allows programmers to access array elements using pointer arithmetic or indexing interchangeably.

  • arr[i] is equivalent to *(arr + i)
  • &arr[0] is equal to the base address 'arr'
  • Pointer arithmetic increment depends on data type size
  • Array names cannot be re-assigned or incremented
  • Pointers provide efficient access to multi-dimensional arrays

Formula Sheet

Address of A[i][j] (Row-Major) = Base + (i * C + j) * Size

Address of A[i][j] (Col-Major) = Base + (j * R + i) * Size

*(ptr + i) == ptr[i]

Exam Tip

Always trace memory allocation manually during the exam to avoid pointer arithmetic errors, especially when dealing with multi-dimensional array base address calculations.

Common Mistakes

  • Forgetting the null terminator '\0' when manually manipulating character arrays
  • Accessing array indices out of bounds leading to undefined memory behavior
  • Confusing the base address of an array with the value stored in the first element

More Revision Notes

Ready to test yourself?

Play topic-wise Arrays & Strings questions in Aspirant Arcade — gamified MCQ practice.

Download Free