Announcement

Collapse
No announcement yet.

zero and null in asciz pointers

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

  • Michael Mattias
    replied
    Related: Passing BYVAL %NULL when an asciiz string is empty, or the address when it's not:
    Code:
    MACRO bvaz(asciizString)          = BYVAL IIF&(lstrlen(asciizString), VARPTR(AsCIIZString), %NULL)
    
      LOCAL sz  AS ASCIIZ * something 
      CALL FunctionWhichNeedsNullWhenStringIsEmptry   (bvaz (sz))
    Functions which REQUIRE 'BYVAL %NULL' and will not work with the address of a null string include most of the ODBC API functions.

    MCM

    Leave a comment:


  • Chris Holbrook
    replied
    Thanks, I'll take it!

    Leave a comment:


  • José Roca
    replied
    Code:
    MACRO mZP2S(p) = IIF$(p, @p, "")
    Usage:

    Code:
    sz = mZP2S(p)

    Leave a comment:


  • Chris Holbrook
    started a topic zero and null in asciz pointers

    zero and null in asciz pointers

    This comes under the headings of "things I should have done years ago" and "pitfalls for the unwary".

    Some C compilers treat a pointer value of zero as a pointer to a null string, and I have got into the lazy habit of letting the compiler sort out a null pointer, and allowing a zero pointer to be equivalent to a null string. This results in an ERROR 9 in PowerBasic, as #DEBUG DISPLAY will show at run time - though without #DEBUG DISPLAY or some other form of error trapping you might not notice it!

    So I'm using this macro instead of testing the pointer every time:

    Code:
    #compile exe
    #dim all
    #debug error on
    #debug display on  '<===================== see what happens if you comment it out?
    '--------------------------------------
    macro mZP2S(p,sz)
        macrotemp stemp
        dim stemp as asciz * 1
        stemp = ""
        if p = 0 then
            sz = stemp
        else
            sz = @p
        end if
    end macro
    '--------------------------------------
    function pbmain () as long
        local i, n as long
        local p as asciz ptr
        local sz as asciz * 64
        local s as string
    
        s = "hello, hello"
        ? @p              '<============= gives an error due to a null pointer
        mZP2S(p, sz)
        ? sz              '<============= no error - string derived by macro from null ptr is null
        p = strptr(s)
        MZP2S(p, sz)
        ? sz
    end function
    Is there a better way?
Working...
X