Why isnt this the same:
as:Code:time_t now = time (0); unsigned char *p = (unsigned char*)&now;
Code:time_t now = time (0); unsigned char *p = static_cast<unsigned char*> &now;
Why isnt this the same:
as:Code:time_t now = time (0); unsigned char *p = (unsigned char*)&now;
Code:time_t now = time (0); unsigned char *p = static_cast<unsigned char*> &now;
You are missing the parenthesis around &now
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I ger error:
error C2440: 'static_cast' : cannot convert from 'time_t *__w64 ' to 'unsigned char *'
Why are you trying to do that? What are you trying to accomplish?
No. You can't do that conversion. Think of static_cast as the explicit request of a cast that the compiler could under the right conditions perform implicitly (there's a few exceptions like casts from void*).
The compiler cannot, and will not, perform a static_cast between those two types. time_t is defined as some sort of integral type (probably a long integer). You are requesting a conversion between pointer to an integral type and pointer to char.
You could probably convert from time_t to char with a necessary loss of data, but what you are requesting is a conversion between pointers. There's no such static_cast unless one of the pointers in question is a void*.
You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.
I believe you are trying to store the current time in char[] format. Being that the case, what you probably want is this:
time() returns a time_t representation of the current system time.Code:char* currtime = asctime(localtime(time()));
localtime() returns a tm struct constructed from a time_t object.
asctime() returns a string representation of the tm struct.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.
I'd wager it's exactly what he's trying to do, as long as he's trying to do the same thing as the code he got that snippet from. It looks suspiciously like something I wrote that puns the value of a time_t into a sequence of bytes for hashing.
My best code is written with the delete key.
Well... on that case he will definitely need reinterpret_cast.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.