Pointer

作者: 驭鹤真人 | 来源:发表于2017-02-27 10:57 被阅读0次

    The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known asaddress-of operator.

    An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with thedereference operator(*). The operator itself can be read as "value pointed to by".


    The reference and dereference operators are thus complementary:

    & is the address-of operator, and can be read simply as "address of"

    * is the dereference operator, and can be read as "value pointed to by"


    Remember that if an array, its name can be used just like a pointer to its first element.


    Const and pointer

    int x;

    int * p1 = &x;  // non-const pointer to non-const int

    const int *  p2 = &x;  // non-const pointer to const int

    int * const p3 = &x;  // const pointer to non-const int

    const int * const p4 = &x;  // const pointer to const int

    const int* p2a = &x;//      non-const pointer to const int

    int const* p2b = &x;// also non-const pointer to const int


    void pointers

    Thevoidtype of pointer is a special type of pointer. In C++,voidrepresents the absence of type. Therefore,voidpointers 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 to by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason, any address in avoidpointer needs to be transformed into some other pointer type that points to a concrete data type before being dereferenced.


    void and nullptr

    Do not confuse 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.

    相关文章

      网友评论

          本文标题:Pointer

          本文链接:https://www.haomeiwen.com/subject/xvjwwttx.html