(
void type of pointer is a special type of pointer. In C++, void
represents the absence of type. Therefore, void
pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereferencing properties).
This gives void
pointers a great flexibility, by being able to point to any data type, from an integer value or a float to a string of characters. In exchange, they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason, any address in a void
pointer needs to be transformed into some other pointer type that points to a concrete data type before being dereferenced.]
,因此 void 指針指向的值是沒有類型的(不確定的長度和值屬性)。
,可以是整數,浮點數甚至字符串。
是(不可以對它們直接使用星號*),因為它的長度是不定的,因此,必須使用類型轉換操作或賦值操作來把void 指針指向一個具體的數據類型。
uninitialized pointers and pointers to nonexistent elements of an array:]
,包括沒有任何有效值的地址——特殊的例子是:沒有初始化的指針和指向數組中不存在的元素,如
p nor q
point to addresses known to contain a value, but none of the above statements causes an error. In C++, pointers are allowed to take any address value, no matter whether there actually is something at that address or not. What can cause an error is to dereference such a pointer (i.e., actually accessing the value they point to). ]
,不管這個地址中是否有值。當從這樣的指針中獲取指向的值時會有錯。
null pointer value. This value can be expressed in C++ in two ways: either with an integer value of zero, or with the nullptr
keyword:]
pointer value.
p and q
are null pointers, meaning that they explicitly point to nowhere, and they both actually compare equal: all null pointers compare equal to other null pointers. It is also quite usual to see the defined constant NULL
be used in older code to refer to the null pointer value:]
。
:;
null pointers with void
pointers! A null pointer is a value that any pointer can take to represent that it is pointing to "nowhere", while a void
pointer is a type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer, and the other to the type of data it points to.]
;一個 void 指針是一種