0
    #include <stdio.h>

#define LENGTH 3

char *words[LENGTH];

int main( int argc, char *argv[] )
{
     char *pc;    // a pointer to a character
     char **ppc;  // a pointer to a pointer character

     printf( "\n------------Multiple indirection example\n" );


 // initialize our array string

   words[0] = "zero";
   words[1] = "one";
   words[2] = "two";

// print each element
   for ( int i = 0; i < LENGTH; ++i )
   {
      printf( "%s \n", words[i] );
   }


   ppc = words; // initialize the pointer to pointer to a character


   for( int i = 0; i < LENGTH; ++i)   // loop over each string
   {
       ppc = words + i;
       pc = *ppc;
           while (*pc != 0) {                   // process each character in a string
            printf("%c ", *pc);             // print out each character of the string individually
            pc += 1;
            }
        printf("\n");

   }




return 0;
}

The part that says " char **ppc " declares a char double pointer. Then it says ppc = word . What is the difference between a regular pointer variable being assigned to words and this double pointer variable? I also don't get why a regular pointer variable would be assigned to have the value of a double pointer variable. Can someone explain this?

2
  • Nitpick: a double pointer usually means a pointer to a double type variable. The term you want here is a pointer to pointer. Commented May 12, 2023 at 14:07
  • There's absolutely no reason to use a global variable here. In fact, it complicates what could be char *words[] = { "zero", "one", "two", NULL } where LENGTH is no longer necessary either. Commented May 12, 2023 at 14:08

2 Answers 2

3

There are two things you need to learn:

  1. All arrays naturally decay to pointers to their first element. So for example as an expression word is really the same as &word[0]. Since word is an array of char * elements, a pointer to such an element must then be char **.

  2. For any pointer (or array) p and index i, the expression p[i] is exactly the same as *(p + i). From this follows that p[0] is the same as *(p + 0) which is the same as *p.

If we now put it all together we start with

ppc = words;

which according to point 1 above must the same as

ppc = &words[0];

It makes ppc point to the first element of the array words.

Then we have

ppc = words + i;

which will then be equal to

ppc = &words[i];

I.e. it makes ppc point to the i:th element in the array words.

Finally we have

pc = *ppc;

which is the same as

pc = ppc[0];

But since ppc points to the i:th element of the array words, the last assignment is really the same as

pc = words[i];

The variable ppc isn't really needed in this program, other than as an example.


To illustrate it, lets "draw" it out...

We have the words array:

+---+     +--------+
| 0 | --> | "zero" |
+---+     +--------+
| 1 | --> | "one"  |
+---+     +--------+
| 2 | --> | "two"  |
+---+     +--------+

In the first iteration of the loop, when i == 0, then we have

ppc = &words[0];

which leads to

+-----+     +---+     +--------+
| ppc | --> | 0 | --> | "zero" |
+-----+     +---+     +--------+
            | 1 | --> | "one"  |
            +---+     +--------+
            | 2 | --> | "two"  |
            +---+     +--------+

Then we have the second iteration, when i == 1 which then give

ppc = &words[1];

which looks like

            +---+     +--------+
            | 0 | --> | "zero" |
+-----+     +---+     +--------+
| ppc | --> | 1 | --> | "one"  |
+-----+     +---+     +--------+
            | 2 | --> | "two"  |
            +---+     +--------+

And exactly the same for the third and last iteration of the loop.

Sign up to request clarification or add additional context in comments.

Comments

0

if you wanted an array of char* (array of words) you will need a char** pointer

so for example: char** wordsArr = {"hello","word"} is an array of char*, and you can treat each cell as a string.

it is different from char* words = "hello world" because if we wanted to print just the first word, printf("%s",words) will print "hello world" where printf("%s",wordsArr[0]) will print "hello"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.