Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

c - Cast char pointer to long

Firstly please forgive my limited c knowledge but how can I cast a char pointer into a long on both 32bit and 64bit platforms? This code gives me a compiler warning C4311 with Visual Studio 2015

char *c;
long l;
l = (long)(c);

Thanks

Edit

Error code is 'type cast': pointer truncation from 'char *' to 'long' Can downvotes let me know why they have downvoted?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't do that if you can avoid it.

C99 (and newer) support a type named uintptr_t. It's an integral type that is capable of holding a pointer value. See stddef.h for more info.

Visual Studio is not known to fully support C99. Hence, you might not get that to work but it's worth a try.

char* c = <some value>;
uintptr_t ptr = (uintptr_t)c;

If you compile the c source file as C++ source, (using /TC compiler option), you should be able to use that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...