Announcement

Collapse
No announcement yet.

Pointer problem

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

  • Pointer problem

    Gary's use of pointers encouraged me to give them a try but

    Code:
    ...
    Global Colors&(), Color_Ptr As Long Ptr
    '
    ...
      Color_Ptr = VarPtr(Colors(0)) '<< is assigned first and appears before next in code
    ...
         Colr = Rnd(0, Max_Colors) 'pick a color
         tmp = @Color_Ptr(Colr) '<< gives 'Missing Declaration Error Color_Ptr
    ...
    I *thought* I was using it as Gary does. The only diff I can see is he uses :

    Code:
    Global tg(), ng() As Byte
    Global tgptr, ngptr As Byte Ptr
    And I am *trying* As Long Ptr.
    The array is Dimmed/filled/assigned okay. What am I doing wrong?

    =====================================
    Who has a harder fight than he
    who is striving to overcome himself.
    Thomas a Kempis
    =====================================
    Last edited by Gösta H. Lovgren-2; 19 Sep 2008, 09:28 PM.
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    Use tmp = @Color_Ptr[Colr]
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Originally posted by José Roca View Post
      Use tmp = @Color_Ptr[Colr]
      Oh ho, square brackets. I hadn't noticed that. Old eyes I guess.

      Thanks, Jose.

      ===============================================
      "Thank you for sending me a copy of your book;
      I'll waste no time reading it."
      Moses Hadas
      ===============================================
      It's a pretty day. I hope you enjoy it.

      Gösta

      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

      Comment


      • #4
        Pointer to 3 dimensional array

        When I try to point to Array(1, 2, 3) I get a missing "]" (square, not curly) error after 2.
        There no problem pointing to an Array(1, 2), just (Array(1, 2, 3)

        Code:
        '... in Setup
            Array_Num_Max = 7
          Dim tRC(Array_Num_Max, Row_Max, Col_Max) 
           Num = 0: tRCPtr0 = VarPtr(tRC(Num, 0, 0))
           Num = 1: tRCPtr1 = VarPtr(tRC(Num, 0, 0))
           Num = 2: tRCPtr2 = VarPtr(tRC(Num, 0, 0))
           Num = 3: tRCPtr3 = VarPtr(tRC(Num, 0, 0))
           Num = 4: tRCPtr4 = VarPtr(tRC(Num, 0, 0))
           Num = 5: tRCPtr5 = VarPtr(tRC(Num, 0, 0))
           Num = 6: tRCPtr6 = VarPtr(tRC(Num, 0, 0))
           Num = 7: tRCPtr7 = VarPtr(tRC(Num, 0, 0))
         
        ' later when referencing
            Num = 0 
        ' 'missing bracket error points to Row_Max - the second dimension)
           @tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max] = 1
        Are Pointers limited to 2 dim arrays?

        ================================================
        "A single, seemingly powerless person who dares
        to cry out the word of truth
        and to stand behind it
        with all of his person and all of his life,
        ready to pay a high price, has, surprisingly,
        greater power, though formally disenfranchised,
        than do thousands of anonymous voters."
        Vaclav Havel
        ================================================
        Last edited by Gösta H. Lovgren-2; 20 Oct 2008, 10:37 AM.
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          > Use tmp = @Color_Ptr[Colr]

          Note this is not the same as specifying an array element number, since offsets "[]" are always zero-based and subscripts "()" are a function of the LBOUND used when the array was allocated.

          I think the correct code for this....
          Code:
          Colr = Rnd(0, Max_Colors) 'pick a color
          tmp = @Color_Ptr(Colr) '<< gives 'Missing Declaration Error Color_Ptr
          is simply
          Code:
          Colr = Rnd(0, Max_Colors) 'pick a color
          tmp = Colors& (Colr)
          ... if 'tmp' is a color value, or ....
          Code:
          Colr = Rnd(0, Max_Colors) 'pick a color
          tmp = VARPTR(Colors& (Colr))
          .. if 'tmp' is supposed to be an address (pointer)
          (eg tmp = the global variable Color_ptr)

          (Insufficient code shown)



          MCM
          Last edited by Michael Mattias; 20 Oct 2008, 11:11 AM.
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Jose's solution solved my original problem, M. The problem I'm having now is pointing to a 3 dimmed array -Array(num, 0, 0). The error indicates (to me) that pointers only work up to 2 dimensions.

            I don't know why that would be the case but it appears to be so. (see code above)
            Code:
              Dim tRC(Row_Max, Col_Max) 
                  tRcPtr = VarPtr(tRC(0,0))
               @tRcPtr[Row Of Row_Max, Col Of Col_Max] = 1 '<< This works
             
             
              Dim tRC(Array_Num_Max, Row_Max, Col_Max) 
              tRCPtr0 = VarPtr(tRC(0, 0, 0))
              @tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max] = 1 '<< This doesn't work
            =========================================
            "The man who goes alone can start today;
            but he who travels with another
            must wait till that other is ready."
            Henry David Thoreau (1817-1862)
            =========================================

            *HDT must have been married.
            It's a pretty day. I hope you enjoy it.

            Gösta

            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

            Comment


            • #7
              >Dim tRC(Array_Num_Max, Row_Max, Col_Max)

              "as what?"

              I think as long as you don't want to pass the entire array as a parameter (unless that has been handled in 9x) there should be no reason you can't..
              Code:
                REDIM   rcPtr (X, Y, Z) AS LONG PTR 
              .. in which case @rcPtr(a, b, c) will be valid.

              > @tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max]

              As far as I can tell, more than one value inside square brackets following a pointer variable name is not supported. (in help do find on 'index' to get there).

              Further, it makes no sense. But this a case for an ARRAY of POINTERS, in which case:
              Code:
               
              @tRcPtr0(Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max)  ' parens
              ... should be valid if tRCPtr0 is an array of "something PTR"

              (Up thru 8x could not pass whole array of ptrs as a procedure parameter).


              MCM
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Gösta,
                don't confuse pointers and arrays, they are 2 different things.
                Arrays are more complicated internally but the compiler handles everything for you so the complexity is hidden.
                Pointers are much more simple and so are faster but they rely on you to do all the calculation of offsets in tables instead of the compiler.

                Are you sure your code works with 2 indexes? The manual says you need a special syntax for 2:
                http://www.powerbasic.com/support/he...al_indexes.htm

                Here's an example that might be of help.

                Paul
                Code:
                'PBCC5.0 program
                FUNCTION PBMAIN () AS LONG
                LOCAL x AS DWORD
                DIM a&(10)        '1 dimension array
                DIM b&(10,20)     '2 dimension array
                DIM c&(10,20,30)  '3 dimension array
                
                FOR r& = 0 TO 10  'fill the arrays with test data
                    a&(r&)=r&
                    FOR t& = 0 TO 20
                        b&(r&,t&)=r&+100*t&
                        FOR y& = 0 TO 30
                            c&(r&,t&,y&)=r&+100*t&+10000*y&
                        NEXT
                    NEXT
                NEXT
                
                LOCAL p AS LONG PTR         ' pointer to demonstrate with
                
                'for 1 dimension array
                PRINT a&(5)
                p=VARPTR(a&(0))
                PRINT @p[5]  'same as a&(5)
                
                'for 2 dimension array
                'p=VARPTR(b&(0,0))
                'PRINT @p[5,6]  'same as b&(5,6)    <--this doesn't compile as pointers are not arrays.
                
                'for 2 dimension array
                PRINT b&(5,6)
                p=VARPTR(b&(0,0))
                PRINT @p[5 OF 10,6 OF 20]  'same as a&(5,6)  Special case of 2 dimension array handled by compiler
                
                'for 3 dimension array
                PRINT c&(5,6,7)
                p=VARPTR(c&(0,0,0))
                PRINT @p[5 + 6*11 + 7*21*11]   'same as c&(5,6,7)  See how we must calculate the pointer offset manually.
                                                'there are 11 elemnts in each first dimension (0 to 10)
                                                'thereare 21 elements in the second dimension (0 to 20)
                
                WAITKEY$
                END FUNCTION

                Comment


                • #9
                  As far as I can tell, more than one value inside square brackets following a pointer variable name is not supported
                  See Help file, Programming Reference\Data Types\Array Data Types\Pointers to arrays with dual indexes.

                  However, beyond dual indexes does not seem to be supported, as the reported error will so indicate, In those instances you can use the method Michael mentions, or perhaps use an absolute array and calculate the offset index yourself. That of course means that you need to understand the column major order of array element storage and use it properly. See Programming Reference\Data Types\Array Data Types\Internal representations of arrays.

                  Once you understand that, you could establish an absolute array (DIM ... AT) and access with a single calculated address. Otherwise establish the array of pointers as Michael suggests, fill and use.

                  Dynamic String arrays will require that you know about string handles, versus where the data is stored. Then you could point to the appropriate string handle and use the value stored there which is a pointer to the actual data. Dynamic arrays in PB are managed so data can be moved as changes occur and the need arises, so using the string handle's pointer is needed to get the current address of the data.
                  Rick Angell

                  Comment


                  • #10
                    Thanks all replies guys. I see I have some more work cut out for me with this pointer business. Will post the work around when/if I get it. {sigh}

                    Bummer PB pointer don't support more than 2 Dim arrays. Oh well ....,

                    I'll reply more fully later tonight.
                    It's a pretty day. I hope you enjoy it.

                    Gösta

                    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                    Comment


                    • #11
                      See Help file, Programming Reference\Data Types\Array Data Types\Pointers to arrays with dual indexes.
                      Not with the SQUARE brackets.

                      This makes no sense:
                      Code:
                        LOCAL P As something PTR 
                      
                         X =    @p[a,b]
                      'a' of how many? "P" is not an array, so multiple subscripts (or whatever you'd call them here) are total nonsense.

                      But this does make sense:
                      Code:
                       REDIM P (X, Y) AS SomeType PTR 
                       Z = @p (Sub1, sub2)
                      Michael Mattias
                      Tal Systems Inc. (retired)
                      Racine WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        MCM,
                        "As What?"
                        Obviously I posted incomplete code. It should have included the Globals where the array was defined (declared?).
                        Code:
                        ' this worked in the previous version
                           Global  tRC(), Trc1(),  Trc2(),  Trc3(),  Trc4(),  Trc5(),  Trc6(),  Trc7(),  Trc8()  As Byte 
                           Global tRcPtr, trc1Ptr, trc2Ptr, trc3Ptr, trc4Ptr, trc5Ptr, trc6Ptr, trc7Ptr, trc8Ptr As Byte Ptr
                        ...
                         Dim tRC(Row_Max, Col_Max) 
                              tRcPtr = VarPtr(tRC(0,0))
                           @tRcPtr[Row Of Row_Max, Col Of Col_Max] = 1 '<< This works
                         '
                        '
                        'This doesn't work in the current version 
                           ' Row Col arrays
                          Global tRC() As Byte
                          Global tRCPtr0, tRCPtr1, tRCPtr2, tRCPtr3, tRCPtr4, tRCPtr5, tRCPtr6, tRCPtr7 As Byte Ptr
                        ...
                          Dim tRC(Array_Num_Max, Row_Max, Col_Max) 
                          tRCPtr0 = VarPtr(tRC(0, 0, 0))
                          @tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max] = 1 '<< This doesn't work
                        While coding the prv version, I was going to use 8 separate arrays. Then I thought "Why not use 1 array with a third dimension?" ie Dim NRC(7, Rows, Cols) and that's where my troubles started [sigh}. And I thought I was being so slick too. {laughing at myself}

                        For now I think I'll go back to the prv ver while digesting what you guys have posted. Though I think using a 3 dim (in this application) is more "elegant" and a lot less work (though I'm sure someone will show me the error of my ways).


                        See Help file, Programming Reference\Data Types\Array Data Types\Pointers to arrays with dual indexes.
                        I looked, Rick. Several times. And found nothing so must have been looking in the wrong place. That said,, however, I had no trouble pointing with a dual index (@tRcPtr[Row Of Row_Max, Col Of Col_Max] = 1), but with a triple index. (@tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max] = 1)

                        Paul, I will look at your example next. Thanks. Just wanted to get this posted before I got lost (again) and it all went out of my head.

                        ==================================
                        "He has all the virtues I dislike
                        and none of the vices I admire."
                        Sir Winston Churchill (1874-1965)
                        ==================================
                        It's a pretty day. I hope you enjoy it.

                        Gösta

                        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                        Comment


                        • #13
                          Attached the code of the version with the 513 Error expecting a [ after the second index.
                          Attached Files
                          It's a pretty day. I hope you enjoy it.

                          Gösta

                          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                          Comment


                          • #14
                            Oh, I forgot the use of the "OF" keyword here, didn't I? I'd better huh?

                            My bad..
                            Indexed pointers with dual [bold mine] indexes require an "OF limit" clause on both indexes.
                            Code:
                            @tRcPtr[Row Of Row_Max, Col Of Col_Max] = 1 '<< This works
                            '
                            @tRcPtr0[Num Of Array_Num_Max, Row Of Row_Max, Col Of Col_Max] = 1 '<< This doesn't work
                            Hmm, looks like... dual indexes are supported, but not triple.

                            "Please Support Triple indexes with pointer offsets " sounds like a new feature suggestion.


                            MCM
                            Last edited by Michael Mattias; 21 Oct 2008, 07:44 AM.
                            Michael Mattias
                            Tal Systems Inc. (retired)
                            Racine WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Originally posted by Michael Mattias View Post
                              Oh, I forgot the use of the "OF" keyword here, didn't I? I'd better huh?
                              Tsk! Tsk!
                              [/quote]
                              Hmm, looks like... dual indexes are supported, but not triple.

                              "Please Support Triple indexes with pointer offsets " sounds like a new feature suggestion.


                              MCM[/quote]
                              Duly noted and sent on to Support this morning.

                              ==========================================================
                              "You are never dedicated to something
                              you have complete confidence in.
                              No one is shouting "The sun is going to rise tomorrow."
                              You KNOW it's going to rise tomorrow.
                              When people are dedicated to political or religious faiths
                              or any other kinds of dogmas or goals,
                              it's always because those dogmas or goals are in doubt."
                              Zen, and the Art of Motorcycle Maintenance
                              R. Pirsig
                              ==========================================================
                              It's a pretty day. I hope you enjoy it.

                              Gösta

                              JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                              LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                              Comment


                              • #16
                                It was the keyword "OF" in mixed case "Of" that threw me. At first I thought it was pseudo-code.

                                BTW, I think you need more lines in that signature. With only 22 you are risking your leadership position.
                                Michael Mattias
                                Tal Systems Inc. (retired)
                                Racine WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  I'd aver that not just triples, but up to the limit of 8 dimensions (subscripts or index numbers). Why stop at 3 ... when you could knock yourself out tracking up to 8 dimensions! Send in a revised NFS since you already asked for triples ... why not ask for the full monte here. Also see Paul's example that apparently came in at the time I was working on my last post. It illustrates the idea of calculating the offset that I was mentioning.

                                  In the help topic Programming Reference\Data Types\Array Data Types\multidimensional arrays we find this umm... "sage advice":

                                  Arrays of four to eight dimensions are possible, but they become more difficult to conceptualize and keep straight. You can define:
                                  DIM five%(5,5,10,20,3) ' a five-dimensional array
                                  but it's probably better to redesign this array into several smaller ones with fewer dimensions, or use an array of User-Defined Types
                                  [Emphasis added]
                                  UDT's can be a real life-saver if the information you need can be stored in this manner. PLUS, you can now use the custom sort feature added to Array Sort in 5/9 to easily sort by UDT members!
                                  Last edited by Richard Angell; 21 Oct 2008, 10:24 AM.
                                  Rick Angell

                                  Comment


                                  • #18
                                    I love the places in the help file where it tells me that even though something is supported, it's too hard to understand so I should do it another way, don't you?

                                    But my favorite is still the "explanation" of the HANDLES option in the SHELL statement/function:
                                    HANDLES
                                    This option, if present, allows the child process to inherit the file handles opened by the parent program. This affects only Windows handles, not PowerBASIC file identifiers. It is an advanced option, for those who understand the need and usage. [italics mine]
                                    Last edited by Michael Mattias; 21 Oct 2008, 09:09 AM.
                                    Michael Mattias
                                    Tal Systems Inc. (retired)
                                    Racine WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Well, Michael, it's unfortunate you don't understand it... but that likely reinforces the notion that you don't really need it for your style of programming.

                                      Best regards,

                                      Bob Zale
                                      PowerBASIC Inc.

                                      Comment


                                      • #20
                                        Well, Michael, it's unfortunate you don't understand it... but that likely reinforces the notion that you don't really need it for your style of programming
                                        For the record, I have an open request in to PB Support for a better explanation.

                                        MCM
                                        Michael Mattias
                                        Tal Systems Inc. (retired)
                                        Racine WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X