Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:28:03+00:00 2026-06-10T04:28:03+00:00

this is my first post on SO, even though i’ve spent some time already

  • 0

this is my first post on SO, even though i’ve spent some time already here.
I’ve got here a problem with a function returning a 2d array. I have defined a private 2d int array property int board[6][7] in my Game class, but i don’t know how to create a public getter for this property.

These are relevant parts of my game.h:

#ifndef GAME_H
#define GAME_H

class Game
{
public:
    static int const m_rows = 6;
    static int const m_cols = 7;

    Game();
    int **getBoard();

private:
    int m_board[m_rows][m_cols];

};

#endif // GAME_H

Now what I would like is something like this in game.cpp (cause I thought array name without brackets is a pointer to first element, obviously it doesn’t work with 2d arrays) :

int **Game::getBoard()
{
    return m_board;
}

So that i can put this for example in my main.cpp:

Game *game = new Game;
int board[Game::m_rows][Game::m_cols] = game->getBoard();

Can anybody help me, what should i put in my game.cpp ?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:28:04+00:00Added an answer on June 10, 2026 at 4:28 am

    You cannot pass arrays by value into and out of functions. But there’s various options.

    (1) Use a std::array<type, size>

    #include <array>
    
        typedef std::array<int, m_cols> row_type;
        typedef std::array<row_type, m_rows> array_type;
        array_type& getBoard() {return m_board;}
        const array_type& getBoard() const {return m_board;}
    private:
        array_type m_board;
    

    (2) Use the correct pointer type.

        int *getBoard() {return m_board;}
        const int *getBoard() const {return m_board;}
    private:
        int m_board[m_rows][m_cols];
    

    An int[][] has no pointers involved. It isn’t a pointer to an array of pointers to arrays of integers, it’s an array of an array of integers.

    //row 1               //row2
    [[int][int][int][int]][[int][int][int][int]]
    

    Which means one int* points to all of them. To get to a row offset, you’d do something like this:

    int& array_offset(int* array, int numcols, int rowoffset, int coloffset)
    {return array[numcols*rowoffset+coloffset];}
    
    int& offset2_3 = array_offset(obj.getBoard(), obj.m_cols, 2, 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here. I have a problem. I need to take
This is my first post even though I've been reading SO for a while.
This is my first post here so go easy. I am trying to build
this is my first post here on stackoverflow and am very impressed by the
this is my first post on this website, but I'm all the time getting
first post here.. I didn't know how to search for this topic because I
This is my first post on SO, so be gentle. I'm not even sure
This is my first post in this forum, so please, be patient with me.
This is my first post and I my first experience with jquery. I have
This is my first post on stackoverflow, so please excuse me if my question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.