0

I'm trying to rewrite some code from C and I came across a pattern I do not understand.

uint8_t buffer[20];

...

uint8_t *ps = buffer+3

What does the +3 mean?

My assumption is this the first three bytes of the buffered array but I'm not sure and I can't seem to find an explanation.

tierfour
  • 682
  • 6
  • 14
  • 2
    That makes no sense. `buffer+3` is equivalent to `&buffer[3]` – Barmar Mar 16 '20 at 21:48
  • 6
    `uint8_t ps = buffer+3` should not compile. Sure it's not `uint8_t* ps = buffer+3`? – NathanOliver Mar 16 '20 at 21:48
  • 1
    @NathanOliver Disagree with [uint8_t ps = buffer+3 should not compile](https://stackoverflow.com/questions/60713628/in-c-what-does-it-mean-to-add-a-number-to-a-buffer-i-e-buffer3#comment107419481_60713628) In C, `buffer+3` is a pointer and a pointer can be assigned to an `uint8_t`. "Any pointer type may be converted to an integer type." C17dr § 6.3.2. 6 It may lead to "the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation". Possible to do, yet likely an error as you suggest. – chux - Reinstate Monica Mar 16 '20 at 22:00
  • @NathanOliver Yes. Not really the heart of the question though. – tierfour Mar 16 '20 at 22:02
  • it's pointer arithmetic: https://www.learncpp.com/cpp-tutorial/6-8a-pointer-arithmetic-and-array-indexing/ – yano Mar 16 '20 at 22:07
  • 1
    @chux-ReinstateMonica Hmm ... When I compile it, I get: initialization of ‘uint8_t’ ... from 'uint8_t *' makes pointer from integer without a cast. For what you're saying, I think, at a minimum, you'd need a cast (e.g.) `uint8_t ps = (uint8_t) (buffer + 3);` but it would still get flagged with: cast from pointer to integer of different size – Craig Estey Mar 16 '20 at 22:17
  • @chux: An initialization must satisfy the constraints for simple assignment (C 2018 6.7.9 11). For assignment to an integer type, the right operand must have arithmetic type (6.5.16.1 1). A compiler is required to diagnose this constraint violation, although it may compile the program in spite of it. – Eric Postpischil Mar 16 '20 at 23:04
  • 1
    @CraigEstey As you say: "When I compile it, I get: initialization ..." a gcc [warning](https://stackoverflow.com/questions/21858412/c-pointers-and-arrays-warning-assignment-makes-pointer-from-integer-without-a/21859173). This differs from NathanOliver assertion [should not compile](https://stackoverflow.com/questions/60713628/in-c-what-does-it-mean-to-add-a-number-to-a-buffer-i-e-buffer3?noredirect=1#comment107419481_60713628). I am not suggesting `uint8_t *ps = buffer+3` is good practice, just compilable code - warts and all. – chux - Reinstate Monica Mar 16 '20 at 23:39
  • @EricPostpischil Unless option "treat warnings as error", do you think `uint8_t *ps = buffer+3;` should not compile? – chux - Reinstate Monica Mar 16 '20 at 23:46
  • @chux: My statements are correct as they stand. If you are trying to teach people learning C or C++, or communicate well generally, then I think you should be more forthcoming—clear and complete—about the behavior to be expected than merely disagreeing that it should not compile, explaining that it may be accepted by a conforming implementation even though it violates a requirement. Certainly a C implementation is free to reject the program. Yes, I think such programs **should** not compile as a default behavior of the compiler; I would prefer to see `-Werror` as the default. – Eric Postpischil Mar 16 '20 at 23:56
  • @EricPostpischil My comment did not [merely disagree](https://stackoverflow.com/questions/60713628/in-c-what-does-it-mean-to-add-a-number-to-a-buffer-i-e-buffer3?noredirect=1#comment107419709_60713628) and it did add "likely an error as you suggest". It backed up with a citation unlike the incorrect [should not compile](https://stackoverflow.com/questions/60713628/in-c-what-does-it-mean-to-add-a-number-to-a-buffer-i-e-buffer3?noredirect=1#comment107419481_60713628). I concur about what we would like in your comment, yet that is not C. – chux - Reinstate Monica Mar 17 '20 at 00:47

1 Answers1

0

In C++ arrays are pointers and the indexing operator (square brackets) makes you access the element in that position: what really happens when you access the element N with buffer[N] is *(buffer + N) and the * at the beginning means to extract the value in that memory address. So uint8_t *ps = buffer+3 saves the address of the element of buffer in position 3. To access the value you just need to type *ps and you'll get the value of the element.

Bonfra
  • 297
  • 1
  • 11