What is the data type of pointer variables?

前端 未结 7 1352
栀梦
栀梦 2020-12-29 12:59

I refereed the following link,

Link1 Link 2

In the above link1 it was mentioned in answer that \"Pointers are of pointer type\".

I jus

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 13:29

    Your question probably refers to the "data type of a pointer", in contrast to the data type of the pointed-to data, which is what one would understand in the first place.

    Based on this assumption, then please look at type uintptr_t or void*.

    To quote Drew Dorman's answer: "uintptr_t is an unsigned integer type that is capable of storing a pointer. Which typically means that it's the same size as a pointer"

    Of course, its size is platform-dependant: 32 bit or 64 bit. So don't transport this variable across platforms of different size.

    Please note, to assign it you have to cast from the 'specific' pointer type, to the 'generic' one:

    int var = 1;
    int* addrOfVar = &var; // pointer to variable
    uintptr_t pVar = (uintptr_t)&var;
    uintptr_t pVar2 = reinterpret_cast(&var); // alternative cast
    

提交回复
热议问题