Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

REDIM vs DIM with Passed Arguments

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • REDIM vs DIM with Passed Arguments

    "in a procedure or function, you can use REDIM to redimension
    an array that was passed as a parameter."

    I have found that this is true only if the original array was
    first REDIMed rather than simply DIMed. If DIM is used, and I
    try to REDIM a passed array, I get an error message "duplicate
    array name" (or similar). However, if I REDIM the array, I can
    then REDIM it within a procedure.

    Anyone know why this is so?

    Thanks,
    Pat


    ------------------

  • #2
    Pat, I suspect it has to be something particular to do with your code... can you post a small example that demonstrates your problem?

    (I often redim arrays that are passed as a parameter)

    Thanks!


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Lance,

      Thanks for a quick response. Here is the code you requested:

      FC$="Allen, Donald,Robert,Charles,"
      redim c$(1)
      call control(C$(),N,FC$)

      sub control(c$(),N,FC$)
      N=TALLY(FC$,",")
      redim C$(N)
      exit sub

      ------------------

      Comment


      • #4
        That code works fine with PB/DOS 3.5... what compiler are you using?

        PB2.x and FirstBasic do not support REDIM of passed arrays.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Duh! I just reread your message... The problem is simple: your array is STATIC and cannot be redimmed, so the compiler flags the REDIM as an error.

          The solution is to explicitly set the array to be dynamic, or set the initial boundaries using a variable (not an equate), or use a REDIM statement instead. This is a documented behavior.
          Code:
          $STATIC           ' the default array type will be STATIC
          DIM DYNAMIC a$(1) ' dynamic as explicitly declared as such
          DIM b$(1)         ' static as boundaries are known at compile-time, and $STATIC is in effect
          DIM c$(%MaxSize)  ' static (as above)
          DIM d$(x%)        ' dynamic as x% not resolvable at compile-time
          DIM STATIC e$(n%) ' not valid - array is declared as static, but boundaries not resolvable at compile-time.
          $DYNAMIC          ' the default array type will be DYNAMIC
          DIM e$(1)         ' dynamic as $dynamic in effect
          DIM f$(%maxsize)  ' dynamic (as above)
          DIM g$(x%)        ' dynamic
          DIM STATIC h$(1)  ' static as explicitly declared as such
          DIM STATIC i$(n%) ' not valid - array is declared as static, but boundaries not resolvable at compile-time.
          Also see $HUGE, etc.

          I hope this helps...!


          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            I thought Pat was on to something so I wouldn't have to upgrade
            from my trusty PB2.1f.

            I wrote this small test program to check his posting with
            absolutely no luck. I tried all possible combinations and it
            still errors (599) out on the REDIM

            $lib all off
            color 14,1
            cls

            i = 4
            dim a$(i)
            i = i + 1
            redim a$(i)

            a$(1) = "The quick "
            a$(2) = "brown fox "
            a$(3) = "jumped over "
            a$(4) = "the lazy dog "
            a$(5) = "1234567890 times."

            locate 1,1
            for x = 1 to 5
            print;a$(x);
            next x
            call test.data(a$(),i)
            end

            sub test.data(a$(),i)
            ' but it will work with: (adjusting parameter line)
            ' sub test.data(i)
            ' shared a$()

            i = i + 1
            redim a$(i)
            a$(1) = "Now is the "
            a$(2) = "time for all "
            a$(3) = "good men to "
            a$(4) = "come to the aid "
            a$(5) = "of their country. "
            a$(6) = "This is a padding line"

            locate 3,1
            for x = 1 to i
            print;a$(x);
            next x
            end sub


            ------------------
            There are no atheists in a fox hole or the morning of a math test.
            If my flag offends you, I'll help you pack.

            Comment


            • #7
              Sorry Mel. As I noted above, PB2.x and FirstBasic do not support REDIM of passed arrays.


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment


              • #8
                Lance and All,
                Thanks. I did not consider the STATIC nature of the matrix.
                Lance,
                I'm using PB3.2 compiler. I just tried DIM DYNAMIC C$(1) and
                it works fine.
                Again, thanks for the help.
                Pat


                ------------------

                Comment


                • #9
                  Lance,
                  The DYNAMIC DIM works with PB3.2. You mentioned that the first
                  way I DIMed the matrix made it STATIC. But actually, DIM doesn't
                  work if the matrix is made STATIC by use of a constant, as in:
                  DIM C$(1).
                  But if I use REDIM C$(1), the sub works. Does this mean that
                  REDIM always makes the matrix REDIMed DYNAMIC, even tho a constant
                  value is used (in this case, 1)? If this is so, what happens to
                  the string space allocated by the compiler at compile time for
                  the STATIC matrix (arrray)? Or is the compiler smart enough to
                  interpret REDIM C$(1) as a DYNAMIC array rather than as STATIC ?
                  I certainly would like to know what the DIM and REDIM is doing
                  behind the scenes.
                  Thanks again.
                  Pat


                  ------------------


                  [This message has been edited by Pat Bruening (edited January 15, 2001).]

                  Comment


                  • #10
                    Since REDIM can only be used on non-STATIC arrays, the compiler produces code for a DYNAMIC array instead.

                    At runtime, an array cannot be erased or redimmed as another type of array.

                    Therefore, you can't use the REDIM STATIC keyword combination, but you can use REDIM DYNAMIC, etc, provided the array type and data type are consistent through-out the scope of the array.

                    I hope this helps.

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      Lance,
                      Thanks again for your quick response. It is quite logical that
                      STATIC arrays cannot be REDIMed, simply because the complier at
                      compile time sets aside memory; whereas DYNAMIC arrays, are, as
                      the name suggests, DYNAMIC (changeable). I found the answer to
                      my original question in the User's Guide:
                      DECLARING static and Dynamic arrays p.103
                      Arrays are dynamic in the following cases:
                      ...
                      If the array appears in an ERASE or REDIM statement.

                      Thanks again for the help.
                      Understanding why is better than merely knowing what. In this
                      case, with your help, I now understand better the differences
                      between STATIC and DYNAMIC arrays.

                      Pat

                      ------------------

                      Comment

                      Working...
                      X