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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:38:16+00:00 2026-06-07T22:38:16+00:00

I am trying to declare two arrays, one 2D and one 1D. I know

  • 0

I am trying to declare two arrays, one 2D and one 1D. I know the dimensions need to be const values. So the const value is assigned from the return value of a function call. That goes well, but when I use the derived value to declare the array, COMPILE errors! WHY???

Here is my code:

int populateMatrixFromFile(string fname) {
std::ifstream fileIn;
int s = determineDimensions(fname);  // return value  (CONST INT)
const int size = s;                  // assign to const

cout << "Value returned from determineDimensions(): " << size << endl;

if (size > 10){
    cout << "Maximum dimensions for array is 10 rows and 10 columns. Exiting" << endl;
    return 1;
}

fileIn.open(fname.c_str(), ios::in); //opened for reading only.

float aMatrix[size][size];  // ERROR
float bMatrix[size];        // ERROR

BUT it works here:

    // assign the pth row of aMatrix to temp

    const int alen = sizeof (aMatrix[p]) / sizeof (float);
    float temp[alen];                                    // WORKS!!!

    for (size_t i = 0; i < alen; i++) {
        temp[i] = aMatrix[p][i];
    }

Thanks for all help.

  • 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-07T22:38:18+00:00Added an answer on June 7, 2026 at 10:38 pm

    The compiler enforces this rule about a constant size of an array because it allocates the needed memory at compile time. In otherwords, all values needed to calculate the size of the array must be known at compile-time. In your first example, this is not the case, so the compiler complains.

    If you really need to have dynamically sized arrays, you should use pointers and the new[] operator to allocate the array. You will also need to remember to use the delete[] operator to return the memory to the system and avoid any memory leaks.

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

Sidebar

Related Questions

I'm trying to implement a function that have to return the minimum value from
i'm trying to declare and allocate memory for two 2d-arrays. However when trying to
I am trying to declare some integer values (used for controlling checkboxes that I
I was trying to declare a callback function in class and then somewhere i
I'm trying to declare a global variable from within a class like so: class
I am trying to detect the collision from two objects. This collision has more
I'm trying to write a glue function between two data types and I can't
I'm trying to call a function with a two-dimensional array of variable size as
I am trying to join two tables. One being the sys.databases table and the
I'm trying to read from two files, and generate output in a third. I

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.