Announcement

Collapse
No announcement yet.

Pointer lesson 2 needed I think :)

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

  • Pointer lesson 2 needed I think :)

    I think I mix up a a few things in my mind for sure



    Code:
    #COMPILE EXE
    #DIM ALL
    
    TYPE TEST
       TX  AS string PTR
    END TYPE 
    
    
    FUNCTION PBMAIN () AS LONG
    LOCAL t1$, blk AS test
    
    t1$="TEST"
    
    blk.TX = strptr(t1$)
    msgbox @blk.TX 
    
    ' both give pointer var error ??
     
    blk.TX = varptr(t1$)
    msgbox @blk.TX        
    
         
    
    END FUNCTION
    To Ask or Not To Ask ?

  • #2
    The "@" must immediately precede the name of a pointer variable:
    Code:
    TYPE TEST
       TX  AS string PTR    ' member 'tx' is a pointer variable
    END TYPE 
    
    FUNCTION PBMAIN () AS LONG
    LOCAL t1$,_
       blk AS test    ' blk is NOT a pointer variable, it is a 'test' 
    
     @blk.TX  <<< invalid, "blk" is not a pointer variable
      [email protected]   <<< valid, "tx" member of blk IS defined as pointer variable
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      MM Thanks ,

      These 2 below grrrr.. cost me e a few hours !
      Now I see it I understand. but that is mostly the case.

      @blk.TX and [email protected]
      To Ask or Not To Ask ?

      Comment


      • #4
        MM This what I needed,

        Use a member of a struct as pointer to a string.
        Is this correct Michael?

        Code:
        #COMPILE EXE
        #DIM ALL
        
        TYPE TEST
           TX AS long ptr      ' member 'tx' is a pointer variable
        END TYPE 
        
        FUNCTION PBMAIN () AS LONG
        LOCAL t1$, blk AS test    
        LOCAL  K as string ptr
        
        t1$="pointers-grr pointers"
         
        blk.tx = strptr(t1$)
        
        blk.tx = blk.tx + 9
        
        K = varptr(blk.tx) : msgbox @K  
        
        END FUNCTION
        Output grr pointers
        To Ask or Not To Ask ?

        Comment


        • #5
          >Is this correct ..?

          Does it work as you intended? Then it's correct.
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Yes , one thing MM

            IF you use the pointer like I did :

            blk.tx = blk.tx + 9

            K = varptr(blk.tx)

            msgbox @K

            Then the string comes out ok in MSGBOX so offset 9 plus, but when you try this:

            [email protected] then it fails, would it not be nice it gives back a new substring Then?
            To Ask or Not To Ask ?

            Comment


            • #7
              If you want substrings, use the provided substring functions, don't increment pointers like that...
              Code:
               LOCAL S  AS AS STRING, T AS STRING
               LOCAL ps AS STRING PTR 
                S   = "Hello World"
                pS = VARPTR (s) 
                T   = MID$(@ps, 7,5) 
                MSGBOX T
              MCM
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Code:
                TYPE TEST
                   TX AS LONG       ' member 'tx' is a pointer variable  (removed pointer)
                END TYPE
                 
                FUNCTION PBMAIN () AS LONG
                  LOCAL t1$, blk AS test
                  LOCAL  K AS ASCIIZ PTR                 'changed from STRING PTR
                  t1$="pointers-grr pointers" + CHR$(0)  'or use ASCIIZ strings
                  blk.tx = STRPTR(t1$)
                  blk.tx = blk.tx + 9
                  K = blk.tx : ? @K
                END FUNCTION
                Code:
                TYPE TEST
                   TX AS LONG
                END TYPE
                 
                FUNCTION PBMAIN () AS LONG
                  LOCAL t1 AS ASCIIZ * 128
                  LOCAL blk AS test
                  LOCAL k AS ASCIIZ PTR
                  t1="pointers-grr pointers"
                  k = VARPTR(t1) + 9
                  ? @k
                END FUNCTION
                Code:
                FUNCTION PBMAIN () AS LONG
                  LOCAL t1 AS STRING
                  LOCAL x AS LONG
                  LOCAL Address AS DWORD
                  t1="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  Address = STRPTR(t1)
                  FOR X = 1 TO LEN(t1)
                   ? CHR$(PEEK(Address+x-1));
                  NEXT
                  ?
                  LOCAL count AS LONG
                  count = 1
                  FOR X = 1 TO LEN(t1)
                    ? PEEK$(address + x-1, count&);
                  NEXT
                  ?
                  ? PEEK$(address,LEN(t1))
                  WAITKEY$
                END FUNCTION
                Last edited by Mike Doty; 19 May 2008, 01:32 PM.
                How long is an idea? Write it down.

                Comment

                Working...
                X