0

I am trying to understand the following code

uchar  abyHeader[100];
i32 = psSHP->nFileSize / 2;    
ByteCopy(&i32, abyHeader + 24, 4);

where

#define ByteCopy( a, b, c ) memcpy( b, a, c )

I understand how memcpy works. However, I am not sure what abyHeader + 24 does to abyHeader. I know that if we have a uchar*, this would skip 24 elements and point to the next element. Does the array work the same?

6
  • 2
    Since you're in C++ you should steer far, far away from using #define. That definition you have there isn't sufficiently robust as the values could be non-trivial. Instead use a function wrapper, even an inline one. If this is code you've been given it's really sloppy and pretty much pointless since it just shuffles the arguments and obscures what's actually happening, impairing understanding. Commented Oct 17, 2019 at 22:43
  • In general terms *(x + n) and x[n] produce the same result, so x + 24 is akin to &x[24] or in other words, adding 24 * sizeof(uchar) to the pointer. Commented Oct 17, 2019 at 22:45
  • Thanks! This code is given to me and I am just trying to understand what is going on. Commented Oct 17, 2019 at 22:52
  • Yes, arrays (abyHeader) decay to pointers in many situations. abyHeader + 24 is the same as &abyHeader[24], so yes, it works the same as with a uchar*. Commented Oct 17, 2019 at 22:55
  • 1
    Any C or C++ worth anything knows what memcpy does. There is no need for a #define macro there. It's equivalent to replacing a steering wheel for a car with a joystick. Commented Oct 17, 2019 at 22:58

1 Answer 1

1

abyHeader + 24 does nothing to abyHeader. That expression in the parameter passes it result as an argument. Does the array work the same because array is actually a pointer to your first position.

Sign up to request clarification or add additional context in comments.

1 Comment

The result is stored as argument, in second parameter. As secondParameter = abyHeader + 24.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.