Yes, pointer arithmetic is scaled by the item size. Works kind of like a
subscript. If foo is an int* then foo+1 points to the next integer. (IOW
foo+4 if you think in assembler terms.)

The basic strategy is to cast it to a char* and then back.

If you would like, here are two template functions that will do the trick
you want to accomplish.

    // Pointer absolute arithmetic
    // This one matches if no explicit typename
    template<typename T> static inline T *PointerAdd(const T *ptr, const int
increment)
    {
        return (T *)( reinterpret_cast<const char *>(ptr)+increment );
    }

    // This one matches if explicit typename
    template<typename T> static inline T *PointerAdd(const void *ptr, const
int increment)
    {
        return (T *)( reinterpret_cast<const char *>(ptr)+increment );
    }

How to use? Here is an example of adding an absolute length to a struct
pointer:

        apfePtr = PointerAdd(apfePtr, apfePtr->apfelen);

Charles


-----Original Message-----
From: IBM Mainframe Discussion List [mailto:IBM-MAIN@LISTSERV.UA.EDU] On
Behalf Of Seymour J Metz
Sent: Thursday, February 8, 2018 8:12 AM
To: IBM-MAIN@LISTSERV.UA.EDU
Subject: Re: Silly C problem adding hex 6C

Isn't pointer arithmetic in C scaled by the item size?

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to