Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 77

C Exercise 77

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Practice pointers to pointers (using multi-level pointers to traverse an array of strings).

**Analysis:** None.

Program source code:

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. All rights reserved.

#include

<

stdio.h

#include

<

stdlib.h

int

main

(

)

{

// Define an array of character pointers to store string constants

const

char

s

[

]

=

{

"

man

"

,

"

woman

"

,

"

girl

"

,

"

boy

"

,

"

sister

"

}

;

// Define a pointer variable that points to a character pointer

const

char

**

q

;

int

k

;

// Traverse the array and output each string through the pointer-to-pointer

for

(

k

=

0

;

k

<

5

;

k

++

)

{

q

= &

s

[

k

]

;

// Make pointer q point to the address of the k-th element in the character pointer array

printf

(

"

%s

\

n

"

, *

q

)

;

// Dereference q to output the corresponding string

}

return

0

;

}

Code explanation:

The above example produces the following output:



man

woman

girl

boy

sister

100 Classic C Language Examples 100 Classic C Language Examples