分享

【例题】指针

 夜的影子 2018-01-31
  1. '' 变量指针:
  2. ''
  3. dim as integer i = 5
  4. dim as integer ptr p
  5.  
  6. = @i
  7. print i, *p
  8.  
  9. ''
  10. ''程序指针:
  11. ''
  12. sub sayhi( )
  13. print "hi"
  14. end sub
  15.  
  16. sayhi( )
  17.  
  18. dim psayhi as sub( ) = @sayhi
  19. psayhi( )
  20.  
  21.  
  22. function add( byval a as integer, byval b as integer ) as integer
  23. return a + b
  24. end function
  25.  
  26. print add( 1, 2 )
  27.  
  28. type AddFn as function( byval as integer, byval as integer ) as integer
  29. dim padd as AddFn = @add
  30. print padd( 1, 2 )
  31.  
  32. ''
  33. '' 指针索引(与数组非常相似):
  34. ''
  35. '' 3个整数分配内存
  36. = callocate( sizeof(integer) * 3 )
  37.  
  38. p[0] = 123
  39. p[1] = 456
  40. p[2] = 789
  41. print p[0], p[1], p[2]
  42.  
  43. deallocate( p )
  44.  
  45. ''
  46. '' 以前的 peek():
  47. ''
  48. dim as any ptr address
  49. dim as byte dat(0 to 3) = { 123, 123, 123, 123 }
  50. dim as integer j = 12345
  51.  
  52. address = @dat(0)
  53. print peek( address )
  54.  
  55. address = @j
  56. print peek( integer, address )
  57.  
  58. ''
  59. '' 指针 to UDTs:
  60. ''
  61. type MyVector
  62. as integer x, y, z
  63. end type
  64.  
  65. dim as MyVector v
  66. dim as MyVector ptr pv = @v
  67. pv->= 1
  68. pv->= 2
  69. pv->= 3
  70.  
  71. print pv->x, pv->y, pv->z
  72. print (*pv).x, (*pv).y, (*pv).z
  73. with *pv
  74. print .x, .y, .z
  75. end with

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约