Thread: error : variable-sized object 'largeArray2' may not be initialized

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    54

    error : variable-sized object 'largeArray2' may not be initialized

    Hi, when i compile my code i get this error : "error : variable-sized object 'largeArray2' may not be initialized"
    I just don't understand this error, any help would be appreciated, thanks in advance.
    Code:
    float give_coefficients_routh_table_and_fill_two_first_lines(int denominator_degree)
    {
      float largeArray2[20][20] = {0};
      int l = 0;
      int c = 0;
      int e = denominator_degree ;
      for ( e = denominator_degree; e>=0; e--)
                {
                float array_of_coefficients[20];
                float d(e);
                cout << "what is the coefficient linked with degree" << e << "?" << endl;
                cin >> d;
                d = array_of_coefficients[e];
                if ( l==2 ){ l = 0; c++;}
    
    
                    float largeArray2[l][c] = {d};
                    l++;
                }
    return largeArray2[20][20];
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by funnydarkvador View Post
    Code:
    float largeArray2[20][20] = {0};
    you're obviously declaring and initializing your array here. on which line does that warning message acctually occur?

    Quote Originally Posted by funnydarkvador View Post
    Code:
    cin >> d;
    d = array_of_coefficients[e];
    I don't know what you're trying to achieve here. you're accepting input into d, and then overwriting that input with the value already stored in array_of_coefficients[e], which is uninitialized.

    Quote Originally Posted by funnydarkvador View Post
    Code:
    float largeArray2[l][c] = {d};
    what you're doing here is declaring a new array called largeArray2, with dimensions l and c. this is a new array, and the compiler doesn't care because it's in a new scope, distinct from where the previous largeArray2 array was declared.

    Quote Originally Posted by funnydarkvador View Post
    Code:
    return largeArray2[20][20];
    this is wrong. this will return a single float value, when I suspect you actually want to return the whole array. unfortunately, the compiler knows that you only declared the array as a float[20][20], and you're trying to return the last element of a float[21][21].

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    The only error is at this line :
    Code:
    floatlargeArray2[l][c] = {d};
    I just want to give a value d (which is different each time in the loop because it is typed in by the user) to each cell of the array largeArray2 but i think the error is due to the fact that variables l and c are incremented, that's why i get the error error : variable-sized object 'largeArray2' may not be initialized

    Please tell me how to do

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The thing is, that line looks like an attempt to declare and initialise a variable length array named largeArray2, which hides the other array of arrays named largeArray2.

    What you probably want to do is to assign the value of d to each element of largeArray2[l]. (By the way, l is a very bad variable name because on some fonts it looks like 1 (one). Your variable names should be descriptive.)

    One way out is to use a simple loop to assign the values. Another option is to #include <algorithm> and use std::fill or std::fill_n. Of course, you would still have the last problem that Elkvis mentioned: your array is local.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Quote Originally Posted by laserlight View Post
    The thing is, that line looks like an attempt to declare and initialise a variable length array named largeArray2, which hides the other array of arrays named largeArray2.

    What you probably want to do is to assign the value of d to each element of largeArray2[l].
    Yes that's what i want to do, but i don't understand your idea to use a simple loop to assign the values, can you explain me a little bit please?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by funnydarkvador
    Yes that's what i want to do, but i don't understand your idea to use a simple loop to assign the values, can you explain me a little bit please?
    Consider this program:
    Code:
    #include <iostream>
    
    int main()
    {
        int numbers[10] = {0};
    
        // Only insert code below this point:
        // Only insert code above this point.
    
        for (int i = 0; i < 10; ++i)
        {
            std::cout << numbers[i] << ' ';
        }
        std::cout << std::endl;
    }
    Insert code in the position indicated such that the program will print:
    Code:
    0 1 2 3 4 5 6 7 8 9
    The code inserted must not print anything by itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    [QUOTE=laserlight;1151378]Consider this program:
    Code:
        for (int i = 0; i < 10; ++i)
        {
            std::cout << numbers[i] << ' ';
        }
        std::cout << std::endl;
    }
    I just don't understand the use of this part of the code : why do i need to display the cells of an array on 1 line :
    1
    0 1 2 3 4 5 6 7 8 9

    i don't say your idea is stupid, it may be right actually, but i don't understand how to use it

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    [QUOTE=funnydarkvador;1151391]
    Quote Originally Posted by laserlight View Post
    why do i need to display the cells of an array on 1 line
    For the purpose of learning. Laserlight gave you an exercise that will help you learn. She is smart enough to avoid giving you a canned solution that you would simply copy, as you would learn nothing.


    Incidentally, variable length arrays are a feature of C (after the 1999 C standard). They are not a feature of C++. What is why the compiler is complaining about your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Thank you for your advice laserlight. I think i don't need to use std::cout because i did #include <cstdlib> at the beginning of my program, so that i use cout whether than std::cout
    Anyway i've discovered some errors in my program, and i'll show you the final version that works

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by funnydarkvador View Post
    I think i don't need to use std::cout because i did #include <cstdlib> at the beginning of my program, so that i use cout whether than std::cout
    That is not correct. std is a namespace; including headers do not change what namespace anything lies in. cout lies in std, always will and always have.
    Therefore, regardless of what headers you include, you must say std::cout.
    Alternatively, you may use

    using std::cout;
    or
    using namespace std;

    And then you can type cout with "std::". The first approach is okay, but the second is not really recommended. It can cause a lot of name collisions. You should brush up your knowledge on namespaces and includes (headers).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit Sized Variable
    By Crosility in forum C Programming
    Replies: 44
    Last Post: 02-24-2011, 02:17 PM
  2. dynamic-variable-sized array
    By simone.marras in forum C Programming
    Replies: 46
    Last Post: 01-05-2009, 10:05 AM
  3. Variable sized objects!!
    By Leojeen in forum C Programming
    Replies: 2
    Last Post: 09-27-2008, 09:34 PM
  4. Creating a variable-sized object
    By thetinman in forum C Programming
    Replies: 5
    Last Post: 08-27-2005, 04:44 AM
  5. Variable-sized Arrays. Help! -:)
    By Bill 101 in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 12:29 AM