Thread: Confused by pointer offset calculation

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    7

    Confused by pointer offset calculation

    I am trying to work out the difference (in bytes) between two members of a packed structure.

    The structure is defined like this:
    Code:
    struct __attribute__((packed)) jcamsys_msg_image
    {
            unsigned char           magic[2];
            uint32_t                msglen;
            uint16_t                msgtype;                                // JC_MSG_... or JC_MSG_...
            uint16_t                reqid;
            uint16_t                cam;
            uint16_t                image_type;                             // at the moment only JC_IMAGE_TYPE_JPEG
            char                    crypted;                                // TRUE if data that follows is encrypted
            uint32_t                datalen;
            uint16_t                width;
            uint16_t                height;
            unsigned char           data[JC_MAX_MSGLEN-JC_MAX_HEADERLEN];   // image data
    };


    Code:
    int jc_msg_image(struct jcamsys_key* key, int *fd, int reqid, int crypted, int cam, unsigned char*data, int datasize, int datalen, int image_type, int width, int height)
    {
            struct jcamsys_msg_image*       msgi =(struct jcamsys_msg_image*)sendbuf;
            int w=0;
    
            msgi->magic[0]  = MAGIC0;
            msgi->magic[1]  = MAGIC0;
    !!!     msgi->msglen    = ( ((uintptr_t)msgi->data - (uintptr_t)msgi->msgtype) ) + datalen;                         // len = remaining header size + data size        
            msgi->msgtype   = JC_MSG_IMAGE;
            msgi->reqid     = jc_nextreqid(reqid);
            msgi->cam       = cam;
            msgi->image_type= JC_IMAGE_TYPE_JPEG;
            msgi->crypted   = crypted;
            msgi->datalen   = datalen;
            msgi->width     = width;
            msgi->height    = height;
    
            if (crypted==TRUE)
                    jcam_crypt_buf_copy(key, msgi->data, data, datasize, datalen);                          // copy as we crypt
            else    memcpy(msgi->data, data, datalen);                                                      // else just copy
    
    printf("sending image message msgi->msglen=%d\n",msgi->msglen); fflush(stdout);
            if (jc_sockwrite(fd, (unsigned char*)&sendbuf, msgi->msglen)<0)
                    return(JC_ERR);
            else    return(msgi->reqid);
    }
    The issue is that:
    ((uintptr_t)msgi->data - (uintptr_t)msgi->msgtype)

    gives some huge number, it should be 15 ?

    What I am trying to say is "as an integer" what the difference between the address of structure member data and address of structure member msgtype.

    Anyone know what I am doing wrong?

    Many Thanks,
    Jon

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    You need to subtract the addresses, not the values.
    Code:
    ((uintptr_t)&msgi->data - (uintptr_t)&msgi->msgtype)
    


    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    That works a treat, thanks :-)

    I am getting careless in my old age...

    Returns 17 not my expected 15 as GCC quite reasonably aligns data[] to a 32 bit boundary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused about using pointer. Please help me!
    By Sakuma in forum C Programming
    Replies: 1
    Last Post: 12-12-2016, 08:28 AM
  2. Stack Pointer offset
    By Eman in forum C Programming
    Replies: 1
    Last Post: 12-11-2010, 04:34 PM
  3. getting confused with pointer to a pointer - 2d array
    By help_seed in forum C++ Programming
    Replies: 0
    Last Post: 12-02-2009, 09:48 PM
  4. Pointer confused... :-(
    By hanez in forum C Programming
    Replies: 5
    Last Post: 08-12-2005, 09:52 AM
  5. confused with scope&pointer
    By cman in forum C Programming
    Replies: 7
    Last Post: 03-23-2003, 03:39 AM

Tags for this Thread