You have three issues here.
C arrays degrade to pointers. You can't return C arrays as such, just a char **
. This does not have a size implied, so you will need a sentinel (e.g. a NULL
) at the end. Or use a std::vector
in C++.
You should not return the address of a local variable from a function as its memory address can be overwritten as soon as the function exits. You will need to malloc
(or new
in C++ to allocate the array. In the example below I made it a global.
You can't have a const
declaration of the array yet return it as non-const without a cast. Assuming you are processing, drop the const
.
Try something like:
#include <stdio.h>
#include <stdlib.h>
// Use char* casts to enable the 'processing' later, i.e. do not use
// const strings
char *Items[] = { (char*) "Item1", (char*) "Item2", (char*) "Item3", NULL };
char **
GetItems ()
{
//processing items..
return Items;
}
int
main (int argc, char **argv)
{
int i;
char **items = GetItems ();
for (i = 0; items[i]; i++)
{
printf ("Item is %s\n", items[i]);
}
exit (0);
}