-1

We recently talked in college about pointers to pointers (e.g: char **ppch) and from my understanding they can be used as 2D arrays since a char *pch, for example, can be used as an array of char/string.

Is that the case or did I miss something? I'm not sure on how we work with P2P in a program or if my understanding is even close to correct.

Can someone provide an example? Do we need to allocate memory with malloc() for each element in these P2Ps?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57

1 Answers1

4

An inherent property of arrays is, that their elements are consecutive in memory. Eg.

int foo[] = { 1, 2, 3, 4, 5, /* ... */ };
// ~> 1, 2, 3, 4, 5, ...

A 2d array is no different:

int bar[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// ~> 1, 2, 3, 4, 5, 6, 7, 8, 9

But a pointer to a pointer of some type

int **qux;

is just that. A pointer to a pointer (or a number of pointers). When you allocate memory like

int **qux = malloc(N * sizeof(*qux));

you get a pointer to a region of memory large enough to hold N pointers to int which in turn can point to other regions of memory:

int **qux = malloc(N * sizeof(*qux));
int value = 0;
for (size_t i = 0; i < N; ++i) {
    qux[i] = malloc(P * sizeof(**qux));
    for (size_t k = 0; k < P; ++k)
        qux[i][k] = ++value;
}

That looks in memory like

+----------+                              +-----+
| qux[0] --|------------------------------|-> 1 |
| qux[1] --|-----------+                  |   2 |
| qux[2] --|---+       |       +-----+    |   3 |
| ...      |   |       +-------|-> 4 |    +-----+   
+----------+   |               |   5 |       
               |    +-----+    |   6 |
               +----|-> 7 |    +-----+
                    |   8 |
                    |   9 |
                    +-----+

So no, a pointer to a pointer is not a 2d-array. Things like that are rather called "jagged array".

As for your example with strings, there is no difference:

char **qux;  // a pointer to a (number of) pointers to char

qux = malloc(N * sizeof(*foo));

// just an array to fill the memory qux points to with:
char const *source[] = { "foo", "bar", "baz", "qux" /* I'm running out of names */ };
size_t index = 0;

for (size_t i = 0; i < N; ++i) {
    qux[i] = malloc((strlen(source[index]) + 1) * sizeof(**qux));
    strcpy(qux[i], source[index++]);
}

~>

+----------+                                                      +--------+
| foo[0] --|------------------------------------------------------|-> 'f'  |
| foo[1] --|-----------------------------------+                  |   'o'  |
| foo[2] --|----------------------+            |    +--------+    |   'o'  |
| foo[3] --|---+                  |            +----|-> 'b'  |    |   '\0' |   
| ...      |   |                  |                 |   'a'  |    +--------+   
+----------+   |    +--------+    |   +--------+    |   'r'  |
               +----|-> 'q'  |    +---|-> 'b'  |    |   '\0' |
                    |   'u'  |        |   'a'  |    +--------+
                    |   'x'  |        |   'z'  |
                    |   '\0' |        |   '\0' |
                    +--------+        +--------+

In contrast a real 2d-array of chars:

char foo[][4] = { "foo", "bar", "baz", "qux" /*, ... */ };
// ~> 'f', 'o', 'o', '\0', 'b', 'a', 'r', '\0', 'b', 'a', 'z', '\0', 'q', 'u', 'x', '\0', ...
Swordfish
  • 12,971
  • 3
  • 21
  • 43