Array Length with Integer Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hacksign23
    New Member
    • Nov 2008
    • 8

    Array Length with Integer Help?

    Well I'm declaring an array with text length,

    so i did
    int i[this->textBox1->textlength];
    but that didn't work so i did
    int q = this->textBox1->textlength;
    int i[q];

    but neither did that work.

    any ideas?

    thanks.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Originally posted by OP
    neither did that work
    Sorry to hear that.
    Originally posted by OP
    any ideas?
    Fix the problem.

    Obviously, we can give you no help because you have provided us with no real information.

    Comment

    • hacksign23
      New Member
      • Nov 2008
      • 8

      #3
      Originally posted by oler1s
      Sorry to hear that.
      Fix the problem.

      Obviously, we can give you no help because you have provided us with no real information.
      Code:
       int q = this->textBox1->TextLength;
      				 int i = 1;
      				 int o[q];
      				 int a = 0;
      				 o[0] = 0;
      Form1.h(105) : error C2057: expected constant expression
      Form1.h(105) : error C2466: cannot allocate an array of constant size 0
      Form1.h(105) : error C2133: 'o' : unknown size
      Form1.h(112) : warning C4800: 'int *' : forcing value to bool 'true' or 'false' (performance warning)

      Comment

      • Tassos Souris
        New Member
        • Aug 2008
        • 152

        #4
        obviously, the compiler you are using does not support allocating arrays with sizes that are not only constants... are you using Visual Studio? cause visual studio does not support the C99 standard at all!!

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          You can't make a static array with a variable size. Better use a dynamic array or a vector.

          Comment

          • hacksign23
            New Member
            • Nov 2008
            • 8

            #6
            Originally posted by boxfish
            You can't make a static array with a variable size. Better use a dynamic array or a vector.
            yea i'm using VC++ Express Edition 2008 edition

            andd.... please don't confuse me with "dynamic array or vector" because i started C++... about 3 hours ago. thanks.

            (a good example would be good enough)

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              Because the compiler doesn't know how much memory to allocate when you don't use a constant expression, it throws an error. To rectify this, use 'new', like this:

              Code:
              int *arr = new int[10];
              //...
              delete arr;
              This allows you to allocate memory in variable sizes. Any pointer/memory you allocate with new MUST be deallocated with delete when you're done with it. Read a good tutorial at http://www.cplusplus.com/doc/tutorial/.

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Originally posted by Laharl
                Code:
                int *arr = new int[10];
                //...
                delete arr;
                Rather delete[] arr;

                Comment

                • Tassos Souris
                  New Member
                  • Aug 2008
                  • 152

                  #9
                  Originally posted by Laharl
                  Because the compiler doesn't know how much memory to allocate when you don't use a constant expression, it throws an error. To rectify this, use 'new', like this:

                  Code:
                  int *arr = new int[10];
                  //...
                  delete arr;
                  This allows you to allocate memory in variable sizes. Any pointer/memory you allocate with new MUST be deallocated with delete when you're done with it. Read a good tutorial at [URL=http://www.cplusplus.c om/doc/tutorial/[/URL].
                  look at the C99 standard
                  the new C++ standard (almost done) will support C99..

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Are you using .NET? A Windows Form? If so then this would be more preferable

                    Code:
                    array<int>^ MyArray = gcnew array<int>(ARRAY_SIZE);
                    Which creates a handle to an array of integers.

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      Originally posted by Tassos Souris
                      look at the C99 standard
                      the new C++ standard (almost done) will support C99..
                      I'm fairly familiar with C99 (and C++0x), but VS does not support C99, nor is there (AFAIK) a way to enable C99 compliance in VS the way -std=c99 does in gcc.

                      Comment

                      • hacksign23
                        New Member
                        • Nov 2008
                        • 8

                        #12
                        oh thanks! that worked.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by Laharl
                          I'm fairly familiar with C99 (and C++0x), but VS does not support C99, nor is there (AFAIK) a way to enable C99 compliance in VS the way -std=c99 does in gcc.
                          Noting that -std=c99 does not even produce strict C99 compliance in the GCC compiler since it has yet to fully implement the C99 standard (although it is getting quite close).

                          Comment

                          Working...