I am given an array of strings in the form of a pointer to a pointer of a character (that is, char **)
There is no such thing: a char**
is just a pointer. It is not an array.
if thing
is char**
then
*thing
is char*
and **thing
is, as declared, a char
.
An array can be seen as a pointer, but a pointer is not an array.
note: the prototype for main
is in general
int main( int argc, char** argv)
so what you need to do is the same as every C program gets from the system.
Example
To have char**
as an array of pointers to char
you must build it
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char* array[] = {
"I", "wish", "to", "access", "each", "element",
"in", "the", "array"
};
int N = sizeof(array) / sizeof(char*);
printf("%d strings in total\n\n", N);
char** strings = (char**)malloc(N * sizeof(char*));
for (int i = 0; i < N; i=i+1)
*(strings+i) = *(array + i);
for (int i = 0; i < N; i=i+1)
printf("%3d %s\n", 1 + i, *(i + strings));
// pointer is not an array
char thing = '@';
char* p = &thing;
char** pp = &p;
printf("\n\nthing is '%c', *p is '%c', **pp is '%c'\n",
thing, *p, **pp);
return 0;
}
output
9 strings in total
1 I
2 wish
3 to
4 access
5 each
6 element
7 in
8 the
9 array
thing is '@', *p is '@', **pp is '@'
In particular note this lines
char** strings = (char**)malloc(N * sizeof(char*));
for (int i = 0; i < N; i=i+1)
*(strings+i) = *(array + i);
where is defined that strings
is an area of the needed size and than the pointers are initialized to the strings of the input array.