MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 1 of 8

Block 1 · Unit 2Arrays, Pointers and Structures9 min · Section 1 of 8

2.1One-Dimensional Arrays

Covers IGNOU MCS-208 §2.0, 2.2

An array lets one name stand for many values of the same type, each reached by its position. This page covers the basic facts you need before anything else: declaration, size, bounds and initialisation.

What an array is

Because the elements are the same size and lie next to each other, the address of element i can be calculated directly: start address + i × element size. That is why reading or writing any element takes the same, constant time, O(1)O(1), whatever the index.

Declaring an array

The general form in C is:

Declaration of a one-dimensional array
data_type array_name[size];
  • data_type is the type of every element (int, char, float, a structure, …);
  • array_name is any valid identifier;
  • size is a positive integer constant expression: the number of elements.

For example, int a[100]; declares 100 integers, a[0] to a[99].

Memory and bounds

The memory needed is

bytes=size of one element×number of elements.\text{bytes} = \text{size of one element} \times \text{number of elements}.

With 4-byte int, int a[100]; needs 4×100=4004 \times 100 = 400 bytes. In a program, write sizeof a for the total and sizeof a / sizeof a[0] for the number of elements; never assume a fixed size for int.

The smallest index is the lower bound (LB) and the largest is the upper bound (UB). In C the lower bound is always 0, so an array of nn elements has indices 00 to n−1n - 1. In general (some languages and many exam questions allow other lower bounds):

number of elements=UB−LB+1.\text{number of elements} = UB - LB + 1.

Initialising an array

Initialisation examples
int a[4] = {34, 60, 93, 2};    /* size given and all values given      */
int b[]  = {2, 3, 4, 5};       /* size left out: taken from the list (4) */
int c[6] = {7, 8};             /* c[2] to c[5] become 0                 */

If an array is initialised when it is declared, its size can be left out: the compiler counts the values. If fewer values are given than the size, the rest are set to 0.

What does an array contain if you give it no values at all? That depends on where it is declared:

Where declaredInitial value of elements
Inside a function, without static (automatic)Indeterminate (“garbage”): reading it is an error
Outside all functions (global), or with static0
With an initialiser list that is too shortThe remaining elements are 0

No bounds checking

C does not check indices. Writing a[100] for an array of 100 elements compiles without error but uses memory that does not belong to the array: this is undefined behaviour, and it can crash the program or silently change other variables. Checking indices is the programmer’s job.

Key takeaways

  • An array is finite, ordered, homogeneous and contiguous; any element is reached in O(1)O(1).
  • Memory = element size × number of elements; number of elements = UB−LB+1UB - LB + 1.
  • In C, indices run from 0 to n−1n - 1; the size may be omitted when an initialiser list is given.
  • Missing initialisers become 0; only uninitialised local (automatic) arrays hold garbage.
  • C does not check bounds: an index outside the array is undefined behaviour.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

An array is declared as A[-5 .. 10] (lower bound −5, upper bound 10). How many elements does it have?

Show answer

Answer: C. 16

UB−LB+1=10−(−5)+1=16UB - LB + 1 = 10 - (-5) + 1 = 16. The answer 15 forgets the “+1”, the same slip as UB−LBUB - LB.

Question 2 · multiple choice

With 4-byte int, how much memory does int marks[25]; need?

Show answer

Answer: C. 100 bytes

4×25=1004 \times 25 = 100 bytes. 50 bytes would be correct only for an old compiler with 2-byte int.

Question 3 · multiple choice

What does this program print?

C · q2-init.c
#include <stdio.h>

int main(void)
{
    int v[5] = {3, 1};
    int total = 0;
    for (int i = 0; i < 5; i++)
        total += v[i];
    printf("%d %zu\n", total, sizeof v / sizeof v[0]);
    return 0;
}
Show answer

Answer: B. 4 5

v[2] to v[4] are set to 0 because the initialiser list is shorter than the array, so the total is 3+1=43 + 1 = 4. sizeof v / sizeof v[0] counts all 5 elements, not only the 2 given.

Question 4 · multiple choice

If an array is initialised at the time of declaration, then:

Show answer

Answer: A. its size may be left out

The compiler counts the values in the list (Check Your Progress 1, Q1: “initialised”). Arrays in C always start at index 0 and are always homogeneous.

Question 5 · multiple choice

What happens when a C program assigns to a[10] for int a[10];?

Show answer

Answer: D. The behaviour is undefined: it writes outside the array.

C does no bounds checking. The write goes to memory after the array and may corrupt other data or crash the program. Arrays in C never grow.

Question 6 · short answer

Define an array. Write the formulas for its memory size and its number of elements, and apply them to float t[50]; (4-byte float).

Show worked answer

An array is a finite, ordered collection of elements of the same type, stored in contiguous memory and accessed by an integer index. Memory = size of one element × number of elements; number of elements = UB−LB+1UB - LB + 1. For float t[50];: LB=0LB = 0, UB=49UB = 49, so 49−0+1=5049 - 0 + 1 = 50 elements, and memory =4×50=200= 4 \times 50 = 200 bytes.

↑ ↓ move↵ openTab switch type