user225549
user225549

Reputation: 187

when const pointer is used as parameter of function

entire code is below link. base64 decode snippet in c++ I have a question about const pointer in above link code.

main

std::vector<BYTE> myData;
...
std::string encodedData = base64_encode(&myData[0], myData.size());

base64_encode

std::string base64_encode(BYTE const* buf, unsigned int bufLen) {
  std::string ret;
  int i = 0;
  int j = 0;
  BYTE char_array_3[3];
  BYTE char_array_4[4];

  while (bufLen--) {
    char_array_3[i++] = *(buf++);
    if (i == 3) {

parameter is BYTE const* buf, not const BYTE* buf.

when const BYTE* buf is used as parameter, const is for BYTE, so pointer can be changed but the value of buffer can not be changed.

when BYTE const* buf is used, const is for pointer variable, so value can be changed but address can not be changed.

in above code, buf pointer is const, but buf++ is possible?

and why BYTE const* buf is used instead of const BYTE* buf?

thanks

Upvotes: 0

Views: 107

Answers (1)

Jack C.
Jack C.

Reputation: 774

Confusingly, const BYTE* and BYTE const* are equivalent to each other. Both are pointers-to-const.

To make the pointer itself const, the formulation is BYTE *const. A const pointer-to-const would be BYTE const *const or const BYTE *const.

I cannot speculate as to why the authors of this function chose the BYTE const* version instead of the much more popular const BYTE*.

Upvotes: 1

Related Questions