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

  • Home
  • SEARCH
  • 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 8678601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:45:46+00:00 2026-06-12T20:45:46+00:00

I want to pass a reference to an array from one object GameModel to

  • 0

I want to pass a reference to an array from one object GameModel to another PersonModel, store reference and then work with this array inside PersonModel just like inside GameModel, but…

I have a terrible misunderstanding of passing an array process: In the class PersonModel I want to pass an array by reference in a constructor (see code block below). But the marked line throws the compile error

    PersonModel::PersonModel( int path[FieldSize::HEIGHT][FieldSize::WIDTH], int permissionLevel ) { 
        this->path = path; //<------ ERROR here:
        //PersonModel.cpp:14:22: error: incompatible types in assignment of 'int (*)[30]' to 'int [31][30]'

        this->permissionLevel = permissionLevel;
    }

Here is the header file PersonModel.h

#ifndef PERSON_MODEL
#define PERSON_MODEL

#include "data/FieldSize.h"

namespace game{
    class IntPosition;
    class MotionDirection;

    class PersonModel {
        protected:
        int path[FieldSize::HEIGHT][FieldSize::WIDTH];
        int permissionLevel;

        public:
        PersonModel( int path[FieldSize::HEIGHT][FieldSize::WIDTH], int permissionLevel );
        void setMotionDirection ( MotionDirection* md);
        void step(long time);
        void reset(long time);
    };
}

#endif 

As I see now, I can change the int path[FieldSize::HEIGHT][FieldSize::WIDTH]; declaration to int (*path)[FieldSize::WIDTH]; but it is much more confusing.

Help me understand this topic: what is the proper way to store the passed reference to an array to work with it later, like with usual 2D array.

UPDATE:

This array is a map of game field tiles properties represented by bit-masks, so it is read-only actually. All the incapsulated objects of GameModel class should read this array, but I definitely don’t want to duplicate it or add some extra functionality.

There are no frameworks just bare Android-NDK.

  • 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-12T20:45:49+00:00Added an answer on June 12, 2026 at 8:45 pm

    I think you’ve fallen into the classic trap of believing someone who’s told you that “arrays and pointers are the same in C”.

    The first thing I’d do would be to define a type for the array:

    typedef int PathArray[FieldSize::HEIGHT][FieldSize::WIDTH];
    

    You then don’t need to worry about confusions between reference to array of ints vs array of references to ints.

    Your PersonModel then contains a reference to one of these.

    PathArray &path;
    

    and, because its a reference it must be initialised in the constructors initialization list rather than in the constructor body.

     PersonModel::PersonModel( PathArray &aPath, int aPermissionLevel ) :
       path(aPath),
       permissionLevel(aPermissionLevel)
     {
     }
    

    Of course, holding references like this is a little scary so you might want to consider using a boost::shared_ptr or something similar instead to make the lifetime management more robust.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Android, I want to pass the reference of currently running MediaPlayer to another
I want to pass parameters from PHP Command Line Interface, and then read in
My data is just number, I want pass it from VC1 to VC2 for
I want to pass an uninitialized object pointer to some method. Within that method
Is it possible to swap a reference of an object that an array element
I want to pass an array of key-value pairs as an argument to a
So I want to extend for example this RecursiveIterator from the SPL with the
I have an array of type string which I want to re-use inside a
Using the dust.js javascript templating engine, I want to pass an array directly: var
I´m trying to pass a reference of array of floats. The problem is the

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.