C Arrays and Pointers (CT 101): Complete IOE Notes & Examples
A visual representation of C Arrays and Pointers in memory
Array and Pointer
6 hours | 6 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 6

C Arrays and Pointers are two of the most powerful and closely related features in the C language. An array is a collection of similar data items stored in contiguous memory locations, while a pointer is a variable that stores the memory address of another variable. Understanding their relationship is key to efficient memory management and advanced programming techniques in C.

Chapter Information

Chapter 6: Array and Pointer (6 hours) – 6 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of C Arrays and Pointers, covering 1D/2D arrays, strings, pointer types, pointer arithmetic, and their relationship, as per the IOE syllabus.

Credit: Sujan Karki

Detailed Chapter Notes

6.1 Array

An array is a fixed-size, sequential collection of elements of the same data type. A single variable name is used to refer to the entire collection, and individual elements are accessed by their index (or subscript).

6.1.1 One Dimensional Array

A one-dimensional array is a linear list of elements.

  • Declaration: data_type array_name[size];
    Example: int scores[50]; declares an integer array that can hold 50 scores.
  • Initialization: Values can be assigned at declaration.
    Example: int numbers[5] = {10, 20, 30, 40, 50};
  • Accessing Elements: Elements are accessed using a zero-based index. For the numbers array above, numbers[0] is 10 and numbers[4] is 50.

6.1.2 & 6.1.3 Two-Dimensional & Multi-Dimensional Array

A two-dimensional (2D) array can be seen as a table or matrix with rows and columns. A multi-dimensional array is an array of arrays.

  • Declaration: data_type array_name[rows][columns];
    Example: int matrix[3][4]; declares a 2D array with 3 rows and 4 columns.
  • Initialization: Can be done linearly or by grouping rows.
    Example: int table[2][3] = {{1, 2, 3}, {4, 5, 6}};

6.1.4 String & String Handling Functions

In C, a string is a one-dimensional array of characters terminated by a null character '\0'.

  • Initialization: char greeting[] = "Hello"; (The compiler automatically adds `\0`).
  • String Handling Functions (from <string.h>):
    • strlen(str): Returns the length of the string.
    • strcpy(dest, src): Copies the source string to the destination.
    • strcat(dest, src): Appends the source string to the end of the destination.
    • strcmp(str1, str2): Compares two strings. Returns 0 if they are equal.

6.1.5 Array of Strings

An array of strings is typically implemented as a 2D character array or an array of character pointers.

Example (using pointers): char *names[] = {"Alice", "Bob", "Charlie"};

6.2 Pointer

A pointer is a variable whose value is the memory address of another variable.

6.2.1 Definition, Declaration & Types

  • Declaration: data_type *pointer_name;
    Example: int *ptr;
  • Address-of Operator (&): Gets the memory address of a variable.
  • Indirection/Dereference Operator (*): Accesses the value at the address stored in a pointer.
int var = 20;
int *ptr;      // Declare an integer pointer
ptr = &var;    // Store the address of var in ptr
printf("Value of var: %d\n", *ptr); // Access value via pointer

Pointer Types:

  • Void Pointer: void * can point to any data type but must be cast before dereferencing.
  • Null Pointer: A pointer that points to nothing, usually initialized with NULL.
  • Bad Pointer: An uninitialized pointer that holds a garbage address. Dereferencing it leads to undefined behavior.
  • Pointer to Pointer: A pointer that stores the address of another pointer (e.g., int **ptr;).

6.2.2 Pointer Arithmetic

You can perform a limited set of arithmetic operations on pointers.

  • Valid: Incrementing (++), decrementing (--), adding an integer, subtracting an integer, and subtracting two pointers of the same type.
  • Invalid: Addition of two pointers, multiplication, or division.

When an integer is added to a pointer, the pointer is incremented by integer * sizeof(data_type) bytes.

6.2.3 Relationship between Pointer and Array

In C, an array name acts as a constant pointer to its first element.

  • array_name is equivalent to &array_name[0].
  • array_name[i] is equivalent to *(array_name + i).

This relationship allows you to use pointer syntax to access and manipulate array elements, which can be more efficient.

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top