0

There is already a lot about this but none solved my problem or maybe I just didn't understand the answers. I'm just simply trying to return an array from a function

Also, I am having to put all of my functions above the main function that also seems weird to me.

And here is what happens when I try to use pointers:

    int * RookMoves(int startingPosition, bool isWhite, int theBoard[64]){
     int startingPositionXY[2] = { startingPosition % 8, (startingPosition - (startingPosition % 8)) / 8 };
     int possibleRookPositions[14];
     int possiblePosXY[2];
     int counter = 0;
     for (int h = 0; h < 2; h++)
     {
         int counter2 = 1;
         for (int j = 0; j < 2; j++)
         {
             counter2 *= -1;
             for (int i = 1; i < 8; i++)
             {
                 int other = startingPositionXY[h] + (i * counter2);
                 int hInverted = (h + abs(h - 1)) * abs(h - 1); // 0 + 1 * 1 = 1 but 1 + 0 * 0 = 0
                 if (other < 8 && other > -1)
                 {
                     possiblePosXY[h] = other;
                     possiblePosXY[hInverted] = startingPositionXY[hInverted];
                     int movesOneDim = possiblePosXY[0] + (possiblePosXY[1] * 8);
                     if (CalculateSameColor(isWhite, theBoard[movesOneDim])) {
                         possibleRookPositions[counter] = movesOneDim;
                         counter++;
                         if (CalculateEnemy(isWhite, theBoard[movesOneDim])) 
                         {
                             break;
                         }
                     }
                     else
                     {
                         break;
                     }
                 }
                 else
                 {
                     break;
                 }
             }
         }
     }

     for (int i = counter; i < 14; i++) //simply changing any unused elements to -1 for later recognition
     {
         possibleRookPositions[i] = -1;
     }
     cout << sizeof(possibleRookPositions) / sizeof(possibleRookPositions[0]) << ' '; // returns 14 just as it should
     return possibleRookPositions;
 }

int main()
{
    int testBoard[64];
    for (int i = 0; i < 64; i++) {
        testBoard[i] = 0;
    }


    int* arr = RookMoves(21, true, testBoard);

    cout << sizeof(arr) / sizeof(arr[0]); //ouputs: 1, should be 14
}

by all things the web says the pointer one should work but it doesn't, it returns an array with a size of 1.

11
  • One question per stackoverflow.com question, please. Commented Aug 4, 2020 at 0:07
  • What web on earth says the pointer should work? You should use std::vector instead. Commented Aug 4, 2020 at 0:07
  • Dividing the sizeofs only works for arrays, not pointers. 1 is correct because you only have one pointer. Commented Aug 4, 2020 at 0:09
  • Also it is bad to return pointers to non-static local variables because they will become invalid on returning from the functions (exiting from their scopes). Commented Aug 4, 2020 at 0:09
  • 1
    @MarkRansom The result of division need not be the number of pointer. For example, if sizeof(int*) is 8 and sizeof(int) is 4 (it is reasonable configuration in 64-bit environment), you will get 2. Commented Aug 4, 2020 at 0:10

2 Answers 2

1

An array in C++, in “simple” code, is either std::vector or std::array. Those can be returned without any problem. I’d say your issue is that you are writing mostly C and calling it C++. C is IMHO much harder to get right for beginners - so use the fact that you got C++ available for your use!

The C-style arrays is something any professional C++ programmer of course fully understands, but whenever I’m forced to write code like that (due to what amounts to customer requirements), it almost never passes the tests on the first try. So don’t be too worried: even people who can write a compiler that could take this array code and produce assembly output still have trouble with getting it right to some extent. It’s unwieldy and it has almost no place in C++ of today.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that makes me feel better lol so say I was making something where speed was very important, wouldn't using this vector library be slower? And I don't get it so am I writing C or how would one write in "C" and should I? Is it faster than C++?
@TristanAlexander: vector is potentially slower than using an array that has the right size. But ìf you already know the right size N, std::array<int, N> is just as fast if not faster than plain arrays. (The "faster" part is for deep technical reasons; the compiler can prove easier that two std::arrays do not overlap)
0

I was trying to get sizeof from a pointer because I don't really understand how those work yet but all I needed to do was to use pointers but just initialize the array I was returning as "static." Thanks to MikeCAT

3 Comments

Declaring the array as static may have made it work for now, but it's a very brittle solution that is likely to cause problems for you down the road.
@Mark Ransom What would the alternative be? Thank you.
Return std::vector<int> instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.