0

To return an char * array from a function I have a following definition.It is not compiling SO I need suggestion from experts to modify it. My ultimate goal is to have char * array from a function.

char * []GetItems()
{
const char *Items[] = {"Item1" , "Item2" , "Item3"};


//processing items..


return Items
}

Thanks for help.

user987316
  • 894
  • 4
  • 13
  • 35
  • i think you should remove "[]" before the GetItems() – MD. Khairul Basar Sep 27 '14 at 10:32
  • 3
    Although it's not what you asked for, it suggest you use a `std::vector` of `std::string`s instead in C++. – blackbird Sep 27 '14 at 10:32
  • 1
    @KhairulBasar which would result in the wrong *type* for the return value. – WhozCraig Sep 27 '14 at 10:32
  • I don't think this is a dupe of "Can a local variable's memory be accessed outside its scope?". The OP's main question is about the return type, i.e. how to return an array of strings. He happens to have illustrated it with a local variable. – abligh Sep 27 '14 at 11:00
  • @abligh It doesn't matter whether title and main question exactly match. The marked dupe has an accepted (and highly upvoted) answer, that explains the problem (exactly the same the OP asks for) in depth. – πάντα ῥεῖ Sep 27 '14 at 16:34

3 Answers3

3

There are three issues here:

  1. To return an array in c/c++ you just use another * and not [].
  2. The return type needs to be marked as const since you're returning a const variable.
  3. Returning a local pointer is almost never a good idea...

So, to get it to compile:

// Note the return type is const char**
const char** GetItems() 
{
    // Note this is still a local variable - you'll have to deal with that
    static const char *Items[] = {"Item1" , "Item2" , "Item3"};

    //processing items..
    return Items;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

You can't have an array as your return parameter in C++ You should either use char** as your return parameter or use std::vector < std::string > > if you are writing C++ code.

Good Luck!

MSH
  • 417
  • 5
  • 11
1

You have three issues here.

  1. 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++.

  2. 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.

  3. 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);
}
abligh
  • 24,573
  • 4
  • 47
  • 84
  • `warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]`. Don't do that. – n. m. could be an AI Sep 27 '14 at 10:49
  • @n.m. : `gcc -Wall` doesn't produce that, but `g++ -Wall` does; I guess that's a C++ vs C difference. – abligh Sep 27 '14 at 10:50
  • The question is tagged C++, not C. – n. m. could be an AI Sep 27 '14 at 10:51
  • @n.m. Yep, but it appeared to be about C types. If he wants to do it the C++ way, OP should be using `std::vector` of `std::string` as I pointed out. Putting `(char *)` before `"Item1"` etc fixes the warning, (and I'll edit the answer to do that) but it really depends what kind of 'processing' OP is doing. – abligh Sep 27 '14 at 10:54
  • Not that you should do that in C either. In C the type of string literals is `char[N]` (not `const`) but they cannot be written to. Which is why you should use `-Wwrite-strings` with gcc. Which is totally irrelevant because the question *is not tagged C*. You might just as well give Haskell or Python advice. – n. m. could be an AI Sep 27 '14 at 11:09