MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 2 of 8

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

2.2Strings and Multidimensional Arrays

Covers IGNOU MCS-208 §2.2

Two kinds of arrays need special attention: arrays of characters, which C uses for text, and arrays with more than one index, used for tables and matrices. This page explains both.

Strings

These two declarations create the same 8-element array:

Two ways to create a string
char message[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '\0'};
char message[] = "example";      /* the compiler adds '\0' itself */

A string literal such as "sentence\n" is stored in memory like this (each box is one byte):

How the string literal is stored in memory
index:  0    1    2    3    4    5    6    7    8    9
       's'  'e'  'n'  't'  'e'  'n'  'c'  'e'  '\n' '\0'

'\0' and '0' are different characters: '\0' has code 0 and ends a string, while '0' is the digit zero with code 48 (in ASCII).

Multidimensional arrays

A multidimensional array has one pair of square brackets for each index:

Declaration of a multidimensional array
data_type array_name[size1][size2] ... [sizeN];

A two-dimensional array int a[3][5]; has 3 rows and 5 columns. Think of it as an array of 3 rows, where each row is itself an array of 5 int:

Elements of int a[3][5]
         col 0    col 1    col 2    col 3    col 4
row 0   a[0][0]  a[0][1]  a[0][2]  a[0][3]  a[0][4]
row 1   a[1][0]  a[1][1]  a[1][2]  a[1][3]  a[1][4]
row 2   a[2][0]  a[2][1]  a[2][2]  a[2][3]  a[2][4]

Its memory is

bytes=size1×size2×size of one element,\text{bytes} = \text{size1} \times \text{size2} \times \text{size of one element},

and for nn dimensions you multiply all nn sizes.

Key takeaways

  • A string is a char array terminated by '\0'; a string literal gets the '\0' automatically.
  • sizeof includes the '\0', strlen does not.
  • '\0' (value 0) is not '0' (value 48).
  • A 2-D array is an array of rows; memory = product of dimensions × element size.
  • Arrays may have any number of dimensions.

Practice questions

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

Question 1 · multiple choice

How many bytes does char city[] = "Khanna"; occupy?

Show answer

Answer: B. 7

6 letters plus the '\0' added by the compiler. 6 would be the value of strlen(city).

Question 2 · multiple choice

What is the integer value of the character '0' in ASCII?

Show answer

Answer: C. 48

The digit zero has code 48. The null character '\0' is the one with value 0.

Question 3 · multiple choice

With 4-byte int, how much memory does int m[4][6][2]; need?

Show answer

Answer: D. 192 bytes

4×6×2=484 \times 6 \times 2 = 48 elements, 48×4=19248 \times 4 = 192 bytes. 48 bytes forgets to multiply by the element size.

Question 4 · multiple choice

What does this program print?

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

int main(void)
{
    char s[20] = "data";
    printf("%zu %zu\n", sizeof s, strlen(s));
    return 0;
}
Show answer

Answer: A. 20 4

sizeof s is the declared size of the array (20 bytes); strlen counts characters before '\0' (4). “5 4” would be right for char s[] = "data";.

Question 5 · multiple choice

“At maximum, an array can be a two-dimensional array.” This statement is:

Show answer

Answer: C. False, C allows any number of dimensions

Each extra pair of brackets adds a dimension: int x[2][3][4][5]; is a valid four-dimensional array.

Question 6 · short answer

Explain how the string "MCA" is stored in memory and write two equivalent declarations for it.

Show worked answer

It is stored as 4 consecutive bytes: 'M', 'C', 'A' and the null character '\0' (value 0) that marks the end. Declarations: char s[] = "MCA"; (the compiler adds '\0') and char s[] = {'M', 'C', 'A', '\0'}; (the terminator written by hand). Both give sizeof s == 4 and strlen(s) == 3.

↑ ↓ move↵ openTab switch type