difference between pointer and reference in c?

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

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

9条回答
  •  Happy的楠姐
    2020-12-25 09:15

    Time to go on a term-bashing spree, because these things always cause confusion.

    • A pointer is a memory address in its own right. Cue fancy diagram for how it happens in memory:

      | Address  | Value          |
      |----------|----------------|
      |0x1111    |0x1112          | <-- Pointer!
      |0x1112    |42              | <-- Pointed value
      |0x1113    |42              | <-- Some other value
      

      I've used a much smaller address size just for simplicity. Basically, 0x1111 is a pointer because its contents are the address of another value.

    • Dereferencing means examining the value of the address held in the pointer's value. Such fancy language can be confusing; basically, if I dereference 0x1111 I look at 0x1112 and get the value out of that address. Why? Because it's really useful and because assembly lets us do it too,

      mov rax, [r8]
      

      Is nasm/intel syntax for "look in r8, find that memory address, follow it and find the value at that memory address and put that in rax".

    • Pass by value. Pass by value means that when you create a function stack frame, which is the stack contents around a function, you copy every value that is an argument to wherever it goes. Registers, stack, wherever. Of course, if you copy a pointer's value, you're copying a memory address and thus creating another pointer pointing to the same memory. This is how functions like this:

      void add(int* x)
      {
          *x = *x + 7;
      }
      

      Work.

    • Pass by reference. What that function above does is essentially pass by reference semantics as you will see them in say C++. The crucial and possibly only difference as the implementation is likely identical at the assembly level is that a reference is something the C++ compiler understands. Since the compiler is the language this is important. C understands pointers and manipulating memory, and so do C compilers, but they'll let you do whatever you like. You can't re-assign a reference, for example,

      void cppadd(int& x)
      {
          int a = 7;
          x = &a; // doesn't work.
      }
      

    So, to sum it up, references are on one level a language feature where the compiler understands where the source memory is and prevents modification of that source memory address. It understands you want to play with the value. Pointers are just that, memory addresses holding other memory addresses.

    Wikipedia summarises it pretty well:

    In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.

    Yes, I have mentioned C++ when this question is only C, but I feel it is prudent to clarify how a term has become somewhat confused with the addition of later languages.

提交回复
热议问题