c中指针和引用之间的区别?

时间:2021-08-29 13:27:57

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

c中的指针,引用和取消引用有什么区别?

8 个解决方案

#1


19  

Here is a memory map; a representation of memory as a sequence of blocks:

这是一张记忆图;将记忆表示为一系列块:

    address    01   02   03
             +----+----+----+...
data within  | 23 | 6f | 4a |
             +----+----+----+...

Now suppose we create a character:

现在假设我们创建了一个角色:

char c = 'z';  // 'z' is 7a in hex

Further suppose c is stored at address 01, so our memory looks like so:

进一步假设c存储在地址01,所以我们的内存如下:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 6f | 4a |
             +----+----+----+...

Now, let's create a pointer:

现在,让我们创建一个指针:

char* p = &c;  // point at c

p may be stored at address 02:

p可以存储在地址02:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 01 | 4a |
             +----+----+----+...

Here the pointer p is at address 02 and it points at address 01. That's the meaning of p = &c;. When we dereference the pointer p (at address 02) we look at what's in the address pointed at by p. That is, p points at address 01, and so dereferencing p means looking inside address 01.

这里指针p在地址02,它指向地址01.这就是p =&c;的含义。当我们取消引用指针p(在地址02)时,我们会查看p指向的地址中的内容。也就是说,p指向地址01,因此解除引用p意味着查看内部地址01。

Finally, lets create a reference:

最后,让我们创建一个参考:

char& r = c;

Here the memory layout doesn't change. That is, no memory is used to store r. r is a sort of alias for c, so when we refer to r we practically refer to c. r and c are conceptually one. Changing r means changing c, and changing c means changing r.

这里的内存布局不会改变。也就是说,没有内存用于存储r。 r是c的一种别名,所以当我们提到r时,我们实际上是指c。 r和c在概念上是一个。改变r意味着改变c,改变c意味着改变r。

When you create a reference you must initialize, and once initialized you cannot re-initialize it with another target. That is, above the reference r means and forever will mean c.

创建引用时,必须初始化,一旦初始化,就无法使用其他目标重新初始化它。也就是说,高于参考r意味着永远意味着c。

Also related are const references. These are the same as a reference, except they are immutable:

const引用也是相关的。这些与引用相同,除了它们是不可变的:

const char& r = c;
r = 'y';  // error; you may not change c through r
c = 'y'   // ok. and now r == 'y' as well

We use const references when we are interested in reading the data but frown upon changing it. By using a const reference the compiler will not copy the data, so this gives us ideal performance, but also forbid us from changing the data, for correctness.

当我们有兴趣阅读数据时我们使用const引用,但是在更改数据时皱眉。通过使用const引用,编译器不会复制数据,因此这为我们提供了理想的性能,但也禁止我们更改数据,以确保正确性。

In a sense, you can say that references are a compile-time feature, whereas pointers are a runtime feature. So references are faster and cheaper than pointers, but come with certain constraints and implications. Like other compile-time-vs-runtime alternatives, we sometimes pick one over the other for performance, sometimes for static analysis and sometimes for flexibility.

从某种意义上说,您可以说引用是编译时功能,而指针是运行时功能。因此,引用比指针更快,更便宜,但具有某些约束和含义。与其他编译时与运行时替代方案一样,我们有时会选择一个用于性能,有时用于静态分析,有时用于灵活性。

#2


6  

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.

    为简单起见,我使用了更小的地址大小。基本上,0x1111是一个指针,因为它的内容是另一个值的地址。

  • 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,

    解除引用意味着检查指针值中保存的地址的值。这种奇特的语言可能令人困惑;基本上,如果我取消引用0x1111,我会查看0x1112并从该地址中获取该值。为什么?因为它真的很有用,因为汇编让我们也这样做,

    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".

    用于“查看r8中的nasm / intel语法,找到该内存地址,跟随它并在该内存地址中找到值并将其放在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,

    通过引用传递。上面的函数基本上是通过引用语义传递的,因为你会在C ++中看到它们。在程序集级别实现可能相同的关键且可能唯一的区别是引用是C ++编译器理解的内容。由于编译器是语言,这很重要。 C理解指针和操作内存,C编译器也是如此,但它们会让你做任何你喜欢的事情。例如,您无法重新分配参考

    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.

在C ++编程语言中,引用是一种简单的引用数据类型,它比从C继承的指针类型更强大但更安全。名称C ++引用可能会引起混淆,因为在计算机科学中引用是一般概念数据类型,带有指针和C ++引用是特定的引用数据类型实现。

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.

是的,当这个问题只是C时,我提到了C ++,但我觉得澄清一个术语如何与后面的语言的添加有些混淆是明智的。

#3


5  

There's no explicit reference type in C like in C++. Anywhere anybody says "reference" in context of C language you can assume a pointer.

在C ++中,C中没有明确的引用类型。任何人在C语言的上下文中都说“引用”,你可以假设一个指针。

#4


2  

C has pointers and you can pretty much do anything you want with those pointers, you can deference them, and you change the value of a pointer. In fact pointer arithmetic is quite common technique in C programming. In my younger days as a C programmer references was not a commonly used term when talking with other C developers.

C有指针,你几乎可以用这些指针做任何事情,你可以尊重它们,并且你改变了指针的值。事实上,指针算法是C编程中非常常见的技术。在我作为C程序员的年轻时代,在与其他C开发人员交谈时,引用并不是常用术语。

References as a term is very commonly used with Java, C# and Object Oriented Languages. In the context of Java and Object Oriented languages a reference is a pointer to an object instance in memory. With a reference you can't do pointer arithmetic, and that is the key difference between pointers and references in my view.

作为术语的引用通常与Java,C#和面向对象语言一起使用。在Java和面向对象语言的上下文中,引用是指向内存中对象实例的指针。使用引用时,您无法进行指针运算,这是我视图中指针和引用之间的关键区别。

Pointers allow for pointer arithmetic and dereferencing, references only allow for dereferencing and changing what the reference points to.

指针允许指针算术和解除引用,引用仅允许解除引用和更改引用指向的内容。

#5


2  

Referencing means taking the address of an existing variable (using &) to set a pointer variable. In order to be valid, a pointer has to be set to the address of a variable of the same type as the pointer, without the asterisk:

引用意味着获取现有变量的地址(使用&)来设置指针变量。为了有效,必须将指针设置为与指针相同类型的变量的地址,而不是星号:

int  c1;
int* p1;
c1 = 5;
p1 = &c1;
//p1 references c1

Dereferencing a pointer means using the * operator (asterisk character) to access the value stored at a pointer: NOTE: The value stored at the address of the pointer must be a value OF THE SAME TYPE as the type of variable the pointer "points" to, but there is no guarantee this is the case unless the pointer was set correctly. The type of variable the pointer points to is the type less the outermost asterisk.

取消引用指针意味着使用*运算符(星号字符)来访问存储在指针中的值:注意:存储在指针地址处的值必须是值相同类型作为指针“指向”的变量类型to,但除非指针设置正确,否则无法保证是这种情况。指针指向的变量类型是最外面的星号。

int n1;
n1 = (*p1);

Invalid dereferencing may or may not cause crashes:

无效的解除引用可能会也可能不会导致崩溃:

Any dereferencing of any uninitialized pointer can cause a crash Dereferencing with an invalid type cast will have the potential to cause a crash. Dereferencing a pointer to a variable that was dynamically allocated and was subsequently de-allocated can cause a crash Dereferencing a pointer to a variable that has since gone out of scope can also cause a crash. Invalid referencing is more likely to cause compiler errors than crashes, but it's not a good idea to rely on the compiler for this.

任何未初始化指针的解除引用都可能导致崩溃使用无效类型转换的解除引用可能会导致崩溃。取消引用指向动态分配的变量并随后取消分配的指针可能会导致崩溃取消引用指向已超出范围的变量的指针也会导致崩溃。无效引用更可能导致编译器错误而不是崩溃,但依赖编译器并不是一个好主意。

References:

http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators

& is the reference operator and can be read as “address of”.
* is the dereference operator and can be read as “value pointed by”.

http://www.cplusplus.com/doc/tutorial/pointers/

& is the reference operator    
* is the dereference operator

you can read wiki as well The dereference operator * is also called the indirection operator.

您也可以阅读wiki。dereference运算符*也称为间接运算符。

This text is taken from this link they have provided the same answer to the same question: meaning of "referencing" and "dereferencing"

本文取自这一链接,他们为同一个问题提供了相同的答案:“引用”和“解除引用”的含义

#6


1  

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.

请注意,C ++引用与C引用不同。 C ++引用是一个抽象的概念,C ++决定允许您对大多数调用使用非指针语法,但在需要时传递指针时会自动“做正确的事”。

#7


0  

Pointer is an address of some data, e.g. int* a. Here a is really just the address where an int value is stored. A reference, in contrast, is another name for some variable, an alias, e.g. int a; int &b = a Here b is just another name for a: b++ has the same effect as a++.

指针是某些数据的地址,例如int * a。这里只是存储int值的地址。相反,引用是某个变量的另一个名称,一个别名,例如, int a; int&b = a这里b只是a的另一个名称:b ++与++具有相同的效果。

#8


0  

as in "C++...":

如在“C ++ ...”中:

In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run-time.

在某些情况下,编译器可以优化远离引用,以便在运行时没有对象表示该引用。

#1


19  

Here is a memory map; a representation of memory as a sequence of blocks:

这是一张记忆图;将记忆表示为一系列块:

    address    01   02   03
             +----+----+----+...
data within  | 23 | 6f | 4a |
             +----+----+----+...

Now suppose we create a character:

现在假设我们创建了一个角色:

char c = 'z';  // 'z' is 7a in hex

Further suppose c is stored at address 01, so our memory looks like so:

进一步假设c存储在地址01,所以我们的内存如下:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 6f | 4a |
             +----+----+----+...

Now, let's create a pointer:

现在,让我们创建一个指针:

char* p = &c;  // point at c

p may be stored at address 02:

p可以存储在地址02:

    address    01   02   03
             +----+----+----+...
data within  | 7a | 01 | 4a |
             +----+----+----+...

Here the pointer p is at address 02 and it points at address 01. That's the meaning of p = &c;. When we dereference the pointer p (at address 02) we look at what's in the address pointed at by p. That is, p points at address 01, and so dereferencing p means looking inside address 01.

这里指针p在地址02,它指向地址01.这就是p =&c;的含义。当我们取消引用指针p(在地址02)时,我们会查看p指向的地址中的内容。也就是说,p指向地址01,因此解除引用p意味着查看内部地址01。

Finally, lets create a reference:

最后,让我们创建一个参考:

char& r = c;

Here the memory layout doesn't change. That is, no memory is used to store r. r is a sort of alias for c, so when we refer to r we practically refer to c. r and c are conceptually one. Changing r means changing c, and changing c means changing r.

这里的内存布局不会改变。也就是说,没有内存用于存储r。 r是c的一种别名,所以当我们提到r时,我们实际上是指c。 r和c在概念上是一个。改变r意味着改变c,改变c意味着改变r。

When you create a reference you must initialize, and once initialized you cannot re-initialize it with another target. That is, above the reference r means and forever will mean c.

创建引用时,必须初始化,一旦初始化,就无法使用其他目标重新初始化它。也就是说,高于参考r意味着永远意味着c。

Also related are const references. These are the same as a reference, except they are immutable:

const引用也是相关的。这些与引用相同,除了它们是不可变的:

const char& r = c;
r = 'y';  // error; you may not change c through r
c = 'y'   // ok. and now r == 'y' as well

We use const references when we are interested in reading the data but frown upon changing it. By using a const reference the compiler will not copy the data, so this gives us ideal performance, but also forbid us from changing the data, for correctness.

当我们有兴趣阅读数据时我们使用const引用,但是在更改数据时皱眉。通过使用const引用,编译器不会复制数据,因此这为我们提供了理想的性能,但也禁止我们更改数据,以确保正确性。

In a sense, you can say that references are a compile-time feature, whereas pointers are a runtime feature. So references are faster and cheaper than pointers, but come with certain constraints and implications. Like other compile-time-vs-runtime alternatives, we sometimes pick one over the other for performance, sometimes for static analysis and sometimes for flexibility.

从某种意义上说,您可以说引用是编译时功能,而指针是运行时功能。因此,引用比指针更快,更便宜,但具有某些约束和含义。与其他编译时与运行时替代方案一样,我们有时会选择一个用于性能,有时用于静态分析,有时用于灵活性。

#2


6  

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.

    为简单起见,我使用了更小的地址大小。基本上,0x1111是一个指针,因为它的内容是另一个值的地址。

  • 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,

    解除引用意味着检查指针值中保存的地址的值。这种奇特的语言可能令人困惑;基本上,如果我取消引用0x1111,我会查看0x1112并从该地址中获取该值。为什么?因为它真的很有用,因为汇编让我们也这样做,

    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".

    用于“查看r8中的nasm / intel语法,找到该内存地址,跟随它并在该内存地址中找到值并将其放在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,

    通过引用传递。上面的函数基本上是通过引用语义传递的,因为你会在C ++中看到它们。在程序集级别实现可能相同的关键且可能唯一的区别是引用是C ++编译器理解的内容。由于编译器是语言,这很重要。 C理解指针和操作内存,C编译器也是如此,但它们会让你做任何你喜欢的事情。例如,您无法重新分配参考

    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.

在C ++编程语言中,引用是一种简单的引用数据类型,它比从C继承的指针类型更强大但更安全。名称C ++引用可能会引起混淆,因为在计算机科学中引用是一般概念数据类型,带有指针和C ++引用是特定的引用数据类型实现。

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.

是的,当这个问题只是C时,我提到了C ++,但我觉得澄清一个术语如何与后面的语言的添加有些混淆是明智的。

#3


5  

There's no explicit reference type in C like in C++. Anywhere anybody says "reference" in context of C language you can assume a pointer.

在C ++中,C中没有明确的引用类型。任何人在C语言的上下文中都说“引用”,你可以假设一个指针。

#4


2  

C has pointers and you can pretty much do anything you want with those pointers, you can deference them, and you change the value of a pointer. In fact pointer arithmetic is quite common technique in C programming. In my younger days as a C programmer references was not a commonly used term when talking with other C developers.

C有指针,你几乎可以用这些指针做任何事情,你可以尊重它们,并且你改变了指针的值。事实上,指针算法是C编程中非常常见的技术。在我作为C程序员的年轻时代,在与其他C开发人员交谈时,引用并不是常用术语。

References as a term is very commonly used with Java, C# and Object Oriented Languages. In the context of Java and Object Oriented languages a reference is a pointer to an object instance in memory. With a reference you can't do pointer arithmetic, and that is the key difference between pointers and references in my view.

作为术语的引用通常与Java,C#和面向对象语言一起使用。在Java和面向对象语言的上下文中,引用是指向内存中对象实例的指针。使用引用时,您无法进行指针运算,这是我视图中指针和引用之间的关键区别。

Pointers allow for pointer arithmetic and dereferencing, references only allow for dereferencing and changing what the reference points to.

指针允许指针算术和解除引用,引用仅允许解除引用和更改引用指向的内容。

#5


2  

Referencing means taking the address of an existing variable (using &) to set a pointer variable. In order to be valid, a pointer has to be set to the address of a variable of the same type as the pointer, without the asterisk:

引用意味着获取现有变量的地址(使用&)来设置指针变量。为了有效,必须将指针设置为与指针相同类型的变量的地址,而不是星号:

int  c1;
int* p1;
c1 = 5;
p1 = &c1;
//p1 references c1

Dereferencing a pointer means using the * operator (asterisk character) to access the value stored at a pointer: NOTE: The value stored at the address of the pointer must be a value OF THE SAME TYPE as the type of variable the pointer "points" to, but there is no guarantee this is the case unless the pointer was set correctly. The type of variable the pointer points to is the type less the outermost asterisk.

取消引用指针意味着使用*运算符(星号字符)来访问存储在指针中的值:注意:存储在指针地址处的值必须是值相同类型作为指针“指向”的变量类型to,但除非指针设置正确,否则无法保证是这种情况。指针指向的变量类型是最外面的星号。

int n1;
n1 = (*p1);

Invalid dereferencing may or may not cause crashes:

无效的解除引用可能会也可能不会导致崩溃:

Any dereferencing of any uninitialized pointer can cause a crash Dereferencing with an invalid type cast will have the potential to cause a crash. Dereferencing a pointer to a variable that was dynamically allocated and was subsequently de-allocated can cause a crash Dereferencing a pointer to a variable that has since gone out of scope can also cause a crash. Invalid referencing is more likely to cause compiler errors than crashes, but it's not a good idea to rely on the compiler for this.

任何未初始化指针的解除引用都可能导致崩溃使用无效类型转换的解除引用可能会导致崩溃。取消引用指向动态分配的变量并随后取消分配的指针可能会导致崩溃取消引用指向已超出范围的变量的指针也会导致崩溃。无效引用更可能导致编译器错误而不是崩溃,但依赖编译器并不是一个好主意。

References:

http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators

& is the reference operator and can be read as “address of”.
* is the dereference operator and can be read as “value pointed by”.

http://www.cplusplus.com/doc/tutorial/pointers/

& is the reference operator    
* is the dereference operator

you can read wiki as well The dereference operator * is also called the indirection operator.

您也可以阅读wiki。dereference运算符*也称为间接运算符。

This text is taken from this link they have provided the same answer to the same question: meaning of "referencing" and "dereferencing"

本文取自这一链接,他们为同一个问题提供了相同的答案:“引用”和“解除引用”的含义

#6


1  

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.

请注意,C ++引用与C引用不同。 C ++引用是一个抽象的概念,C ++决定允许您对大多数调用使用非指针语法,但在需要时传递指针时会自动“做正确的事”。

#7


0  

Pointer is an address of some data, e.g. int* a. Here a is really just the address where an int value is stored. A reference, in contrast, is another name for some variable, an alias, e.g. int a; int &b = a Here b is just another name for a: b++ has the same effect as a++.

指针是某些数据的地址,例如int * a。这里只是存储int值的地址。相反,引用是某个变量的另一个名称,一个别名,例如, int a; int&b = a这里b只是a的另一个名称:b ++与++具有相同的效果。

#8


0  

as in "C++...":

如在“C ++ ...”中:

In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run-time.

在某些情况下,编译器可以优化远离引用,以便在运行时没有对象表示该引用。