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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:44:14+00:00 2026-05-13T06:44:14+00:00

hi I want to do something like this: int op(string s1, string s2){ int

  • 0

hi I want to do something like this:

int op(string s1, string s2){
    int x = s1.size();
    int y = s2.size();
    int matrix = new int[x][y]
    /* do stuff with matrix */
}

For some reason I get the following errors:

SuperString.cpp(69) : error C2540: non-constant expression as array bound
SuperString.cpp(69) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
SuperString.cpp(71) : error C2109: subscript requires array or pointer type

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-05-13T06:44:15+00:00Added an answer on May 13, 2026 at 6:44 am

    Here is a summary of how to build a 2d array in C++ using various techniques.

    Static 2D Matrix:

    const size_t N = 25; // the dimension of the matrix
    
    int matrix[N][N]; // N must be known at compile-time.
    // you can't change the size of N afterwards
    
    for(size_t i = 0; i < N; ++i)
    {
        for(size_t j = 0; j < N; ++j)
        {
            matrix[i][j] = /* random value! */;
        }
    }
    

    Dynamic 2d Matrix:

    const size_t N = 25; // the dimension of the matrix
    int** matrix = new int*[N]; // each element is a pointer to an array.
    
    for(size_t i = 0; i < N; ++i)
        matrix[i] = new int[N]; // build rows
    
    for(size_t i = 0; i < N; ++i)
    {
        for(size_t j = 0; j < N; ++j)
        {
            matrix[i][j] = /* random value! */;
        }
    }
    
    // DON'T FORGET TO DELETE THE MATRIX!
    for(size_t i = 0; i < N; ++i)
        delete matrix[i];
    
    delete matrix;
    

    Matrix using std::vector:

    // Note: This has some additional overhead
    // This overhead would be eliminated once C++0x becomes main-stream ;)
    // I am talking about r-value references specifically.
    typedef vector< vector<int> > Matrix;
    typedef vector<int> Row;
    
    const size_t N = 25; // the dimension of the matrix
    Matrix matrix;
    
    for(size_t i = 0; i < N; ++i)
    {
        Row row(N);
    
        for(size_t j = 0; j < N; ++j)
        {
            row[j] = /* random value! */;
        }
    
        matrix.push_back(row); // push each row after you fill it
    }
    
    // Once you fill the matrix, you can use it like native arrays
    for(size_t i = 0; i < N; ++i)
    {
        for(size_t j = 0; j < N; ++j)
        {
            cout << matrix[i][j] << " ";
        }
    
        cout << endl;
    }
    

    3d matrix using boost::multi_array (taken from boost multi_array docs):

    // Note that this is much more efficient than using std::vector!
    int 
    main () {
      // Create a 3D array that is 3 x 4 x 2
      typedef boost::multi_array<double, 3> array_type;
      typedef array_type::index index;
      array_type A(boost::extents[3][4][2]);
    
      // Assign values to the elements
      int values = 0;
      for(index i = 0; i != 3; ++i) 
        for(index j = 0; j != 4; ++j)
          for(index k = 0; k != 2; ++k)
            A[i][j][k] = values++;
    
      // Verify values
      int verify = 0;
      for(index i = 0; i != 3; ++i) 
        for(index j = 0; j != 4; ++j)
          for(index k = 0; k != 2; ++k)
            assert(A[i][j][k] == verify++);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 335k
  • Answers 335k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As the error message says, the method distanceTo() doesn't exist… May 14, 2026 at 3:42 am
  • Editorial Team
    Editorial Team added an answer I implemented this myself and I only had to do… May 14, 2026 at 3:42 am
  • Editorial Team
    Editorial Team added an answer It is most likely related to tile-based rendering, which divides… May 14, 2026 at 3:42 am

Related Questions

HI. This is what I want to do: str2 = 91; str1 = 19;
HI i want to implement this C code in batch file int i; scanf(%d,
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I
Hi I have a database with loads of columns and I want to insert
I want to do something like this: const MyFirstConstArray: array[0..1] of string = ('Hi',

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.