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:
Is there a better way?
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
Comment