MCS-201 · Unit 7

Pointers

Section 7 of 7

Block 2 · Unit 7Pointers13 min · Section 7 of 7

7.7Pointers and Strings

Covers IGNOU MCS-201 §7.9

A string is an array of characters ending in '\0', so everything about arrays and pointers applies to strings. Character pointers give a compact way to refer to strings and to handle lists of strings of different lengths.

Two ways to hold a string

Declarations
char country1[] = "INDIA";    /* an array of 6 characters, initialised with a copy of the text */
char *country2  = "INDIA";    /* a pointer to a string literal stored elsewhere */
char country1[] = "INDIA";char *country2 = "INDIA";
What is createdAn array of 6 charA pointer (8 bytes) to a string literal
Can the characters be changed?Yes: country1[0] = 'i';No: modifying a string literal is undefined behaviour
Can it refer to another string later?No: an array cannot be reassignedYes: country2 = "CHINA";
sizeof68

Because the literal must not be changed, such a pointer is best declared const char *.

printf("%s", country2) needs the pointer; %s prints characters from that address up to '\0'. *country2 is a single character, the first one, printed with %c.

Walking through a string with a pointer

Array of pointers to strings

char *country[] = {
country[0] ----> "INDIA"
country[1] ----> "CHINA"
country[2] ----> "BANGLADESH"
country[3] ----> "PAKISTAN"
country[4] ----> "U.S"

Each element is a char * pointing to the first character of one string. Compared with a two-dimensional array such as char country[5][11];, which reserves 11 bytes for every row (55 bytes), the strings here occupy only as many bytes as they need, plus one pointer each.

  • country[i] is the i-th string (use with %s).
  • country[i][j], or *(*(country + i) + j), is character j of string i. For example, *(*(country + 3) + 2) is the third character of the fourth string: the ‘K’ of “PAKISTAN”.

Key takeaways

  • char s[] = "…" creates a modifiable array; char *p = "…" points to a literal that must not be modified.
  • %s takes the pointer; *p is one character.
  • A string is traversed by moving a pointer until '\0'.
  • char *list[n] holds n strings of any length; swap pointers to reorder them.

Practice questions

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

Question 1 · multiple choice

For char *p = "HELLO";, what does *(p + 1) give?

Show answer

Answer: C. ‘E’

p + 1 points to the second character, and * gives the character there, ‘E’. Printing p + 1 with %s would give “ELLO”.

Question 2 · multiple choice

Which statement correctly prints the string pointed to by char *name?

Show answer

Answer: B. printf("%s", name);

%s needs the address of the first character, which is name itself. *name is a single character.

Question 3 · multiple choice

Which is true of char a[] = "CAT"; but not of char *p = "CAT";?

Show answer

Answer: D. Its characters may be modified

The array holds its own copy of the characters, which can be changed. The pointer refers to a string literal, which must not be modified.

Question 4 · multiple choice

For char *day[] = {"MON", "TUE", "WED"};, what is day[2][1]?

Show answer

Answer: C. ‘E’

day[2] is “WED” and index 1 of that string is ‘E’.

Question 5 · multiple choice

What does this program print?

C · ptr_len.c
#include <stdio.h>

int main(void)
{
    const char *s = "IGNOU";
    const char *p = s;
    while (*p != '\0')
        p++;
    printf("%td\n", p - s);
    return 0;
}
Show answer

Answer: C. 5

p moves until it reaches '\0', five characters after s; subtracting the pointers gives the length, 5.

Question 6 · short answer

Write a function my_strlen(const char *s) using only pointer operations, and explain why sorting an array of pointers to strings is faster than sorting a two-dimensional character array.

Show worked answer

int my_strlen(const char *s) { const char *p = s; while (*p != '\0') p++; return (int)(p - s); }. The pointer p starts at the first character and advances until it reaches the null character; the difference p - s is the number of characters passed. Sorting an array of pointers exchanges only the pointers (8 bytes each), whatever the length of the strings, while sorting a two-dimensional array must copy whole strings character by character (for example with strcpy) each time two rows are exchanged.

↑ ↓ move↵ openTab switch type