Question: I would like to ask a question about "foreach loop" in PHP. Towards the bottom of the code below, the "while" loop accesses elements from an array individually. How can I rewrite the BODY of the while loop using a "foreach" loop? 

```
  <?php
  require_once('MDB2.php');
  $db = MDB2::connect("mysql://mk:mk@shark.company.com/mk");
  if(PEAR::iserror($db)) die($db->getMessage());
  $sql = "SELECT courses.ID,courses.Name,staff.Name
  FROM courses,staff
  WHERE courses.Coordinator=staff.id
  and courses.ID = \"$course\"";
  $q = $db->query($sql);
  if(PEAR::iserror($q)) die($q->getMessage());
  while($row = $q->fetchRow()){
  echo "<tr><td>$row[0]</td>\n";
  echo "<td>$row[1]</td>\n";
  echo "<td>$row[2]</td></tr>\n";
  }
  }
  ?>
 </table>
 </body></html>

```

Answer: 
If you dereference a `char **`, you should get a pointer to `char`. There are no pointers in a `char[3][10]`. They're just not interchangeable.

It works for a one-dimensional `char *` array because the array name implicitly converts to a pointer to the first element in this context, and if you dereference a `char *` you get a `char`, and that's exactly what your one-dimensional `char` array contains. 

The line of thinking that "if it works for a one-dimensional array, shouldn't it work for a two-dimensional array?" is just invalid, here. 

To make what you want to do work, you'd have to create `char *` arrays, like so:

```
#include <stdio.h>

void fn(int argc, char ** argv) {
    printf("argc : %d\n", argc);
    for (int i = 0; i < argc; ++i) {
        printf("%s\n", argv[i]);
    }
}

int main(void) {
    char * var3[3] = { "arg1", "argument2", "arg3" };
    char * var4[4] = { "One", "Two", "Three", "Four" };

    fn(3, var3);
    fn(4, var4);

    return 0;
}

```

which outputs:

```
paul@local:~/src/c/scratch$ ./carr
argc : 3
arg1
argument2
arg3
argc : 4
One
Two
Three
Four
paul@local:~/src/c/scratch$ 

```

An alternative is to declare your function as accepting an array of (effectively a pointer to) 10-element arrays of `char`, like so:

```
#include <stdio.h>

void fn(int argc, char argv[][10]) {
    printf("argc : %d\n", argc);
    for (int i = 0; i < argc; ++i) {
        printf("%s\n", argv[i]);
    }
}

int main(void) {
    char var3[3][10] = { "arg1", "argument2", "arg3" };
    char var4[4][10] = { "arg1", "argument2", "arg3", "arg4" };

    fn(3, var3);
    fn(4, var4);

    return 0;
}

```

but this obviously requires hard-coding the sizes of all dimensions except the leftmost, which is usually less desirable and flexible. `char argv[][10]` in the parameter list here is another way of writing `char (*argv)[10]`, i.e. declaring `argv` as a pointer to array of `char` of size `10`.
A `type**` is by definition a pointer to a pointer, or an array of pointers.

When used in a function declaration, `type[][]` is inteligible with `type**` as in:

```
int main(int argc, char argv[][]) { ...

```

But it is not when declaring variables. When you do this:

```
char var[a][b] = { ... }

```

This is a matrix of `char` and there are no pointers envolved here. The compiler pretty much handles this as a straight forward array of `char var[a*b]` and when you access `var[1][2]` it internally does `var[1*b+2]`. It is not really an array of strings, and its utilization is only possible because the compiler knows the value of `b` when compiling this function, as the variable was declared there.

In order to pass as `char**`, `var` must be an array of pointers. A list containing pointers of `char*`.

When you do this:

```
char *var[a] { ... }

```

Now you are telling the compiler your intention. `var` is no longer a matrix of `char`, it is truly a list of pointers `char*`.