1

i want to create a function(in a project) that returns an array. I m not quite sure about how can i do that.

int worker::*codebook(UnitType type){
    int  code[12]; 
    if  (type == UnitTypes::center){
        int temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
        code=temp;
    }
    return code;
}

where worker is the class and unitType an enumeration. I define the function in the header file as follows:

int *codebook(UnitType type);

My problems is the following:

cannot convert from 'int' to 'int Worker::*

Any idea about this??

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • possible duplicate of [C++ return array from function](http://stackoverflow.com/questions/8745260/c-return-array-from-function) – Bo Persson Feb 07 '12 at 16:16

8 Answers8

5

The first problem in the code is the syntax. The signature should be:

 int* worker::codebook(UnitType type)

Then, it is assigning to an array:

code=temp;

This is just not allowed by the language.

Finally, it's returning a pointer to a local variable:

return code;

The array will no longer exist when the function returns, so any attempt to use it from outside will result in undefined behaviour.

Now, to answer the main question, how to properly return an array from a function?

One option is to use std::vector:

 std::vector<int> worker::codebook(UnitType type) {
     std::vector<int> code(12); // vector with 12 zeros
     if  (type == UnitTypes::center){
         code[11] = 1;
     }
     return code;
 } 

Another is to use std::array:

 std::array<int, 12> worker::codebook(UnitType type) {
     std::array<int, 12> code = {{}};
     if  (type == UnitTypes::center){
         code[11] = 1;
     }
     return code;
 }
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
2

You can't return a local array, it will go out of scope when the function exits.

It's better to use an actual data structure, such as std::vector perhaps. IF you want to use bare C-level arrays, you must allocate the memory dynamically and add a way for the function to express the length of the array, perhaps by also taking a size_t& length argument.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
unwind
  • 391,730
  • 64
  • 469
  • 606
2

You have error in function prototype:

int worker::*codebook(UnitType type){

should be

int* worker::codebook(UnitType type){

And it is not correct since code is allocated on stack and destructed when it goes out of scope.

You should allocate this code array on the heap. Body of this function could then look like this:

int* code = new int[12]; 

if  (type == UnitTypes::center){

    int temp[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
    memcpy(code, temp, 12*sizeof(int));
}
return code;

but then the caller should call delete[] when it finishes with this array:

int* array = codebook(type);
delete[] array;

Ugly memory management is connected with this kind of solution. Since you are using C++ you should use some object that will make it easier (for example std::vector).

Hope this helps.

LihO
  • 41,190
  • 11
  • 99
  • 167
1

The signature should be:

int* worker::codebook(UnitType type)

Note that you're encountering undefined behavior.

int  code[12]; 

is a local variable, which will get destroyed when the function exits. And you're returning it. Never do this. You should allocate the array dynamically, via new.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

This should be int* worker::codebook(UnitType type). The * goes with the int, to make it a pointer-to-int (int*).

However, you really don't want to return code like that - when the function exits, code will point to garbage (because it is allocated on the stack).

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • Well... To be picky: `code` does NOT point to garbage just because it's allocated on the stack. If it was so the stack would be useless. It's because `code` is a locally allocated array and it goes out of scope when the function returns. – Sani Huttunen Feb 07 '12 at 14:45
  • @SaniHuttunen That's why I said "when the function exits". Typo. – Borealid Feb 07 '12 at 15:59
0

Your pointer declaration is in the wrong place.

 int* worker::codebook(UnitType type)

However, be warned that you are creating your array on the stack, which will get destroyed when your function exits. You need to create it on the heap with new, and remember to delete it when you're done.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0

You don't want to do this because here the array memory is allocated inside the function and will be gone when the function finishes. Allocate the memory/array ouside the function and pass the array in as a pointer, in one of the parameters.

SoftDeveloper
  • 160
  • 1
  • 8
0

You can bury the array inside a struct and then return the struct by value - the classic C style:

struct my_array
{
int code[12];
};

my_array worker::codebook(UnitType type){
    my_array arr = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; 
    if  (type == UnitTypes::center){
       arr.code[11] = 1;
    }
    return arr;
}
celavek
  • 5,575
  • 6
  • 41
  • 69