difference between pointer and reference in c?

前端 未结 9 1546
傲寒
傲寒 2020-12-25 08:11

what is the difference between pointer , reference and dereference in c?

9条回答
  •  情书的邮戳
    2020-12-25 09:15

    A pointer's value is a memory address.

    int a;
    int* b = &a;
    // b holds the memory address of a, not the value of a.
    

    A reference is a pointer with a value (memory address) that refers to a desired item.

    int a;
    int* b = &a;
    // b is a reference to a.
    

    A dereference is a technique of grabbing the memory contents that a pointer references.

    int a;
    int* b = &a;
    int c = *b;
    // c dereferences b, meaning that c will be set with the value stored in the address that b contains.
    

    Note that a C++ reference is a different thing than a C reference. A C++ reference is an abstract idea where C++ decides to allow you to use non-pointer syntax for most calls, but will automatically "do the right thing" in passing a pointer when needed.

提交回复
热议问题