I'm trying to run the following code but it's not compiling. What am I doing wrong?
Code:void func(void* ptr) { static_cast<char*>(ptr); cout << ptr[0]; }
I'm trying to run the following code but it's not compiling. What am I doing wrong?
Code:void func(void* ptr) { static_cast<char*>(ptr); cout << ptr[0]; }
Try This...this will compile...but at runtime it will most probably fail..because trying to access uninitialised ptr
Code:void *ptr; char *p; p=static_cast<char*>(ptr); cout << p[0];
"Service of the poor and destitutes is the service of the God"
Normative Changes to ISO/IEC 9899:1990 in Technical Corrigendum 1
Incompatibilities Between ISO C and ISO C++
Thanks. Your post pointed out my problem. Here is the code:
Code:void func(void* ptr, int numbytes, char value) { static_cast<char*>(ptr); cout << static_cast<char*>(ptr)[i]; }
There is no need for the first line in your function. A static_cast (or any other cast) doesn't modify the variable you pass to it, it returns the casted variable. So to get it to work, you need to save the return value like sunnypalsingh did, or use the return value like the second line in your function does. If you do neither, the cast has no effect, which is why your first line is useless.
Oops. My mistake. Thanks for pointing that out. I had that in there for testing purposes and forgot to take it out.Originally Posted by Daved