No announcement yet.

Converting char*

  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting char*

    Good Day!

    I've run into a small problem in converting some C++ code. This is a portion of the original TYPE:
    typedef struct mon {
       short iNameL;           // name size
       short iNameL2;          // name size again
       char* pName;            // name
    ...following the PB help file, I've converted that into:
    TYPE Monsters
       iNameL AS INTEGER      ' NAME SIZE
       iNameL2 AS INTEGER     ' NAME SIZE again
       pName AS ASCIIZ PTR    ' NAME
    ...unfortunately, it appears that my pName isn't correct. Reading a bit more I learn that char* may be a pointer--which I know very little of. I can tell you that pName's length should be iNameL. Any ideas?

    Don Ewald
    mailto:[email protected][email protected]</A>
    Donnie Ewald
    [email protected]

  • #2
    Your conversion is correct.

    References to [email protected] will retrieve a string, teminating in the first NUL character found.

    What exactly is wrong here? I do UDT members AS ASCIIZ PTR (no length) all the time.

    Note, you might need Pb/win 7+, I'm not sure 6x supported the 'ambiguous length' asciiz ptr.

    But... with 6x you can always make that pointer something like..

      pName     AS ASCIIZ PTR * 32768 (or larger)
    All that will do is limit the maximum length of the string you can retrieve. It still uses only 4 bytes of storage in your program.

    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      Greetings Michael!

      Well, I may still be misunderstanding; but, my program crashes when I add the @ symbol to my PRINT statement. It crashes right during "PRINT [email protected]"...any thoughts?

      DEFDWD A-Z
      TYPE Monsters
        iNameL            AS INTEGER         ' Name size
        iNameL2           AS INTEGER         ' Name size again
        pName             AS ASCIIZ PTR      ' Name
        iAttack           AS INTEGER         
        iDefense          AS INTEGER         
        iUniqueID         AS INTEGER         
        iHP               AS INTEGER         ' hit points
        cGroups           AS BYTE            ' number of groups
        cU5               AS ASCIIZ  *   5   
        cFLevel           AS BYTE            ' find level
        cTrapP            AS BYTE            ' trap %
        iReFire           AS INTEGER         ' resists
        iReCold           AS INTEGER         
        iReElectrical     AS INTEGER         
        iReMind           AS INTEGER         
        iReDisease        AS INTEGER         
        iRePoison         AS INTEGER         
        iReMagic          AS INTEGER         
        iReStone          AS INTEGER         
        iReParalysis      AS INTEGER         
        iReDrain          AS INTEGER         
        iReAcid           AS INTEGER         
        cU7               AS ASCIIZ  *   2   
        fAbSeeInvisible   AS SINGLE          ' abilities
        fAbInvisible      AS SINGLE          
        fAbResistMagicAll AS SINGLE          
        fAbResistCharm    AS SINGLE          
        fAbResistWeapon   AS SINGLE          
        fAbResistCWeapon  AS SINGLE          
        cU8               AS ASCIIZ  *   4   
        fAtPoison         AS SINGLE          
        fAtDisease        AS SINGLE          
        fAtParalyze       AS SINGLE          
        fAtBreathFire     AS SINGLE          
        fAtBreathCold     AS SINGLE          
        fAtSpitAcid       AS SINGLE          
        fAtElectrocute    AS SINGLE          
        fAtDrain          AS SINGLE          
        fAtStone          AS SINGLE          
        fAtAge            AS SINGLE          
        fAtCriticalHit    AS SINGLE          
        fAtBackstab       AS SINGLE          
        fAtDestroyItem    AS SINGLE          
        fAtSteal          AS SINGLE          
        fAtBehead         AS SINGLE          
        cU9               AS ASCIIZ  *   4   
        iSpFire           AS INTEGER         ' spells
        iSpCold           AS INTEGER         
        iSpElectrical     AS INTEGER         
        iSpMind           AS INTEGER         
        iSpDamage         AS INTEGER         
        iSpElement        AS INTEGER         
        iSpKill           AS INTEGER         
        iSpCharm          AS INTEGER         
        iSpBind           AS INTEGER         
        iSpHeal           AS INTEGER         
        iSpMovement       AS INTEGER         
        iSpBanish         AS INTEGER         
        iSpDispell        AS INTEGER         
        iSpResistant      AS INTEGER         
        iSpVisual         AS INTEGER         
        iSpMagical        AS INTEGER         
        iSpLocation       AS INTEGER         
        iSpProtection     AS INTEGER         
        iSpMDamage        AS INTEGER         
        iSpMDeath         AS INTEGER         
        iSpMAlchemy       AS INTEGER         
        iSpMHealing       AS INTEGER         
        iSpMMovement      AS INTEGER         
        cU10              AS ASCIIZ  *   2   
        fRadius           AS SINGLE          ' Size; very small = 0.09, small = 0.18....
        cU3               AS ASCIIZ  *   8   
        cAlignment        AS BYTE            
        cU11              AS BYTE            
        cNumber           AS BYTE            
        cU6               AS BYTE            
        iMoney            AS LONG            
        cU4               AS ASCIIZ  *   4   
        iCLevel           AS INTEGER         
        iStr              AS INTEGER         
        iInt              AS INTEGER         
        iWis              AS INTEGER         
        iCon              AS INTEGER         
        iCha              AS INTEGER         
        iDex              AS INTEGER         
        cU13              AS ASCIIZ  *   2   
        iClass            AS INTEGER         
        cU12              AS ASCIIZ  *   12  
        iBreath           AS INTEGER         ' 0 = air, 1 = water, 2 = both
        cU1               AS ASCIIZ  *   96  
      END TYPE
        'Ignore first 12 bytes. NEXT IS "file version", "file revision" AND "number of monsters". THEN
        '"number of monsters" times the monster structure which can be seen below.
        DIM cHeader   AS ASCIIZ   * 12  ' header
        DIM cVersion  AS ASCIIZ   * 8   ' file version
        DIM iRev      AS INTEGER        ' file revision
        DIM iMonsters AS INTEGER        ' number OF monsters
        DIM Mon       AS Monsters       
        OPEN "DEMISEMonsters.DED" FOR BINARY AS #1
          GET #1, 1, cHeader
          GET #1, 13, cVersion
          GET #1, 21, iRev
          GET #1, 23, iMonsters
          Spot& = 25
          FOR I& = 0 TO iMonsters
            GET #1, Spot&, Mon
            PRINT LEN(Mon)
            PRINT Mon.iNameL
            PRINT [email protected]
            PRINT Mon.iAttack
            PRINT Mon.IDefense
            PRINT Mon.iUniqueID
            Spot& = Spot& + LEN(Mon)
          NEXT I&
        CLOSE #1

      Don Ewald
      mailto:[email protected][email protected]</A>
      Donnie Ewald
      [email protected]


      • #4
        With your usage presented, it is an invalid pointer and will crash every time.

        If you're retrieving data from a file, you'll need to pull the actual string.

        Whatever data it's pulling is a 4 byte pointer off in memory "la-la" land, and Windows don't like that one bit.

        Adam Drake
        Drake Software


        • #5
          Thanks for the replies folks!

          The problem that I have, Adam, is that I still don't understand. I was hoping that I could easily convert the C++ TYPE into a PB one. You tell me that I'll need to pull the actual string; but, I don't know how. I thought that the original code designated a pointer?

          Don Ewald
          mailto:[email protected][email protected]</A>
          Donnie Ewald
          [email protected]


          • #6
            That original C++ type can't be getting that pointer from a file, either. (Well, OK, it "could" but that would be the second-dumbest thing I've ever seen in my life).

            Betcha it gets that from a call.

            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]


            • #7
              Well, I was able to churn something that's not too pretty, but it works. Right now, that's all that matters.

              Don Ewald
              mailto:[email protected][email protected]</A>
              Donnie Ewald
              [email protected]


              • #8
                I thought that the original code designated a pointer?
                char* pName;
                does designate a pointer.

                So does
                pName AS ASCIIZ PTR
                The problem is, where is the actual string?

                [This message has been edited by Greg Lyon (edited December 16, 2005).]