1

I'm not familiar in C++ casting and I want to convert my C style casting to a C++ casting. Here is my code,

typedef unsigned char u8;
u8 sTmp[20] = {0};

//.. code to put string data in sTmp

char* sData;
sData = (char*)&(sTmp[0]);

Here, I want to convert (char*)&(sTmp[0]) to a C++ casting.

Many thanks.

domlao
  • 15,663
  • 34
  • 95
  • 134

1 Answers1

3

Your cast is unnecessarily complicated. You get the first element of the array and then the address of that element. On expressions, arrays decay into pointers, so you can get the address of the array by its name alone:

sData = (char*)sTmp;

Like @Richard said above, the best way to do the cast on C++ is using reinterpret_cast, like this:

sData = reinterpret_cast<char*>(sTmp);

Finally, sTemp (like I already mentioned) will decay to a pointer on expressions, specifically an unsigned char* (which is the usual way of addressing raw memory), so it is very likely that you don't actually need to cast it to char* at all. (unless you have to print it, which doesn´t seem right anyway)

imreal
  • 10,178
  • 2
  • 32
  • 48