Announcement

Collapse
No announcement yet.

POINTER QUESTION - Yet another!

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

  • POINTER QUESTION - Yet another!

    I have a STRING and I want to be able to get each character by its index from the beginning of the string.
    How do I get it so I can get the letters one by one by one by location? So I can get "c" when I look for POINTER + INDEX (I guess this is zero indexed)

    It would be nice to know how to do this for a STRING or STRING*26 or ASCIIZ*26 or ARRAY(26) -- did I forget any?

    Code:
    FUNCTION PBMAIN () AS LONG
    
         LOCAL WORK_STRING AS STRING
         LOCAL WORK_STRING_POINTER AS STRING POINTER
         LOCAL RESULT_STRING AS STRING
    
         WORK_STRING = "abcdefghijklmnopqrstuvwxyz"
    
         WORK_STRING_POINTER = STRPTR(WORK_STRING)
    
         RESULT_STRING = @WORK_STRING_POINTER  + 3  'WILL THIS RETURN "c" NO IT ERRORS
    
    END FUNCTION


  • #2
    Does this help?
    Code:
    FUNCTION PBMAIN () AS LONG
    
      LOCAL WORK_STRING AS STRING
      LOCAL WORK_STRING_POINTER AS STRING POINTER
      LOCAL RESULT_STRING AS STRING
    
      WORK_STRING = "abcdefghijklmnopqrstuvwxyz"
    
      WORK_STRING_POINTER = STRPTR(WORK_STRING)
    
      RESULT_STRING = PEEK$(WORK_STRING_POINTER+2, 1)
    
      ? RESULT_STRING
    END FUNCTION

    Comment


    • #3
      Code:
      LOCAL WORK_STRING_POINTER AS STRING POINTER
      ..
      RESULT_STRING = @WORK_STRING_POINTER  + 3
      You declare the pointer as a string pointer so it returns a string .. then you try to add 3 to that string.
      You can't add a number to a string.

      You need to declare the pointer to point to the type of variable you want it to return. In this case probably STRING*1
      Then you access the characters by adding the offset in []
      Code:
      FUNCTION PBMAIN () AS LONG
      
           LOCAL WORK_STRING AS STRING
         '  LOCAL WORK_STRING_POINTER AS STRING POINTER
           LOCAL WORK_STRING_POINTER AS STRING POINTER *1
      
           LOCAL RESULT_STRING AS STRING
      
           WORK_STRING = "abcdefghijklmnopqrstuvwxyz"
      
           WORK_STRING_POINTER = STRPTR(WORK_STRING)
      
      
           RESULT_STRING = @WORK_STRING_POINTER[3]  'WILL THIS RETURN "c" NO IT ERRORS  .. returns "d" as it's zero based
      
      
      END FUNCTION

      Comment


      • #4
        Very helpful!

        Comment

        Working...
        X