CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    278

    Can anyone give me a general idea of what this code does?

    I have very limited experience in C++ but. . . I have to figure out what a particular part of a Visual C++ v_6's source code is doing in order to mimic it in another language.


    Code:
          // (the 2 lines below) I know these are variable declarations but they're still a bit foreign to me?
          WORD *pWord; 
          WORD *pAppVal= (WORD *)&Buffer[3];
          
          // I understand the three lines below this comment.
          buf[0]= 'H';
          buf[1]= 'A';
          buf[2]= 'P';
    
          // I don't know what these four lines do?
          pWord= (WORD *) &buf[3];
          *pWord++= *pAppVal;
          *pWord++= 0;
          *pWord++= 2 + strlen( pTestStr );

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can anyone give me a general idea of what this code does?

    Imo, you're better off trying to understand what the functionality of the app was rather than trying to interpret some funky code.

  3. #3
    Join Date
    Aug 2010
    Posts
    47

    Re: Can anyone give me a general idea of what this code does?

    Firstly, I find that code to be terrible. Go flog the person who wrote it. Incrementing pointers while you assign things to them is pretty unreadable to anyone who doesn't do it all the time.

    In order to figure out what's going on with your code I had to make a couple assumptions. Basically I'm assuming that buf and Buffer are both (more or less?) null terminated strings. I'm not really sure if they are, but he's treating them like it and you didn't give much info about them.

    Code:
    // So it starts out by assigning the letters HAP to the first three characters of buf. 
     buf[0]= 'H';
     buf[1]= 'A';
     buf[2]= 'P';
    
     // He gets the address of the fourth character
     pWord= (WORD *) &buf[3];
     // Then he takes the fourth and fifth characters of Buffer and puts those
     // into the next two characters of buf. (we're doing 2 characters per "word")
     *pWord++= *pAppVal;
     // Then he adds a null termination as the next two characters
     *pWord++= 0;
     // And lastly he puts two plus the length of the string pTestStr as the next two characters
     *pWord++= 2 + strlen( pTestStr );
    Altogether a baffling way of doing something that seems pretty simple. I'm confused by him putting the pTestStr string length after the null termination, but then maybe that's part of some C++ convention or structure that I just haven't run across yet.

    So just as an example, if Buffer were 'abcdefg' and pTestStr was 'test', then you'd end up with buf being something like:
    HAPde0006

    I think.

    Edit: Oh yeah, remember that as the length of pTestStr goes above 9 that when you view it as a string you won't see a number any more. You'll see characters. Basically the last two characters combine to form the number, which will be stored in two characters worth of bytes. (Not sure how much you know about this sort of thing, so thought I'd mention it.)
    Last edited by Ankheg; September 16th, 2010 at 10:20 AM. Reason: Added edit: bit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured