Converting a primitive data type to a void pointer type

I read about pthreads here . In one example, they provided this source code.

When creating a stream, they pass a long type whose type is used as the void * type for the function !.

Inside the function, they get this value and the inverse to get a long value.

Q1: Is it allowed to convert a pointer type to a primitive data type and vice versa (In C and C ++)?

Q2. If so, is it good to do this? Shouldn't they create a pointer to this long type, and then type this pointer as void * and pass it to the function.

Is this idea of ​​converting a primitive type to a pointer type very confusing? Converting from any pointer type to void * in an understandable, but how primitive data type is stored in void * type? Is it not possible that a particular system size of a primitive type may be larger than the size allocated for the pointer type?

+4
source share
1 answer

Q1: yes, but this is a specific implementation (= platform dependent) whether this will work. A version that would be a bit safer would use uintptr_t instead of long .

Q2: This is a definitively bad style. This is not so important if you are doing it right by highlighting long and passing the address.

+5
source

Source: https://habr.com/ru/post/1440474/


All Articles