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 6014611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:43:01+00:00 2026-05-23T02:43:01+00:00

I have a series of NX objects (e.g., a static bool array of dimension

  • 0

I have a series of NX objects (e.g., a static bool array of dimension [NX][N1][N2] in the example below).

I would like to loop over these objects. At each iteration, an object called ‘A’ is to function as a surrogate for the corresponding element ‘B[x]’ in the series.
However, the code inside the loop is legacy code, so we cannot really change how we refer to ‘A’ itself

    const int NX = ...;
    const int N1 = ...;
    const int N2 = ...;
    bool B[NX][N1][N2];

    Code_needed: declare A here (maybe first defining a class template?)

    int main(){

      for(int x = 0; x < NX; ++x){
        Code_needed: make A refer to B[x] here (maybe A = &B[x])
        // In the following, "A[i][j]" should refer to B[x][i][j]...
        A[3][5] = true; // This is legacy code, so we cannot change it
                        // (e.g., cannot add a * in front of A)
      }        
    }

Note that the types of objects on which I want to apply this are heavy, such as arrays of containers, so copying them over inside the loop is not acceptable. Maximizing performance is generally of interest in this application.

Would appreciate the help!!

EDIT: How would the answer be affected, if A were to be a scalar (i.e., it is B[NX] only)?

  • 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-05-23T02:43:02+00:00Added an answer on May 23, 2026 at 2:43 am

    You can define a reference to a two-dimensional array inside the for loop:

    bool (&A)[N1][N2] = B[x];
    

    Now A[i][j] is equivalent to B[x][i][j].

    Note that you cannot move the definition of A outside the for loop, because references have to be initialized when they are defined, and they cannot be rebound later.

    If you need to define A outside the for loop and reassign inside the for loop, use a pointer instead:

    // outside the for loop
    bool (*A)[N2];
    
    // inside the for loop
    A = B[x];
    

    And here is a complete code example which compiles just fine on my compiler:

    const int NX = 3;
    const int N1 = 5;
    const int N2 = 7;
    
    bool B[NX][N1][N2];
    bool (*A)[N2];
    
    int main()
    {
        for (int x = 0; x < NX; ++x)
        {
            A = B[x];
            A[3][5] = true;
        }
    }
    

    You can also write an alias class template which overloads the assignment operator appropriately:

    template <typename T>
    class alias
    {
        T* p;
    
        alias(const alias&);
        alias& operator=(const alias&);
    
    public:
    
        alias() : p(0) {}
    
        void rebind(T& x)
        {
            this->p = &x;
        }
    
        void operator=(const T& x)
        {
            *p = x;
        }
    
        operator T&()
        {
            return *p;
        }
    };
    

    This works with arrays:

    bool B[NX][N1][N2];
    alias<bool[N1][N2]> A;
    // ...
    A.rebind(B[x]);
    A[3][5] = true;
    

    And plain bools:

    bool B[NX];
    alias<bool> A;
    // ...
    A.rebind(B[x]);
    A = true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of datetime objects and would like to calculate the average
I have a series of performance tests I would like to show as a
I have an array of objects looking like this obj.date=23/02/2010, obj.regType=0, obj.value=1000; obj.date=23/03/2010, obj.regType=0,
I have a series of objects and each has meta-data associated with it. In
What I would like to do is have a static factory function that you
I have a series of objects I have created: Item Order Song etc. Each
I have a timer executing a loop executing a series of threads. Each thread
I have a series of calculations i'm doing over a bunch of objects stored
Okay I have a series of objects based on a base class which are
I have a series of Extension methods to help with null-checking on IDataRecord objects,

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.