C语言 C中“引用”和“取消引用”的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14224831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:58:15  来源:igfitidea点击:

Meaning of "referencing" and "dereferencing" in C

cpointersreferencedereference

提问by Milkncookiez

I read different things on the Internet and got confused, because every website says different things.

我在互联网上阅读了不同的内容并感到困惑,因为每个网站都说了不同的内容。

I read about *referencing operator and &dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to. So I got confused.

我阅读了有关*引用运算符和&取消引用运算符的信息;或者引用意味着使指针指向变量,而取消引用是访问指针指向的变量的值。所以我很困惑。

Can I get a simple but thorough explanation about "referencing and dereferencing"?

我能得到一个关于“引用和取消引用”的简单而彻底的解释吗?

回答by A B

Referencingmeans 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

Dereferencinga pointer means using the * operator (asterisk character) to retrieve the value from the memory address that is pointed by the 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 guaranteethis is the case unless the pointer was set correctly. The type of variable the pointer points to is the type less the outermost asterisk.

取消引用指针意味着使用 * 运算符(星号字符)从指针指向的内存地址中检索值: 注意:存储在指针地址处的值必须是与类型相同的值指针“指向”的变量,但不能保证是这种情况,除非指针设置正确。指针指向的变量类型是最外层星号后的类型。

int n1;
n1 = *p1;

Invalid dereferencingmay or may not cause crashes:

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

  • Dereferencing an 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 referencingis 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

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/

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

& is the reference operator    
* is the dereference operator

http://en.wikipedia.org/wiki/Dereference_operator

http://en.wikipedia.org/wiki/Dereference_operator

The dereference operator * is also called the indirection operator.

回答by Chris Dodd

I've always heard them used in the opposite sense:

我一直听到他们在相反的意义上使用它们:

  • &is the reference operator -- it gives you a reference (pointer) to some object

  • *is the dereference operator -- it takes a reference (pointer) and gives you back the referred to object;

  • &是引用运算符——它给你一个指向某个对象的引用(指针)

  • *是解引用运算符——它接受一个引用(指针)并返回被引用的对象;

回答by ApproachingDarknessFish

For a start, you have them backwards: &is reference and *is dereference.

首先,您将它们倒置:&是引用和*取消引用。

Referencing a variable means accessing the memory address of the variable:

引用变量意味着访问变量的内存地址:

int i = 5;
int * p;
p = &i; //&i returns the memory address of the variable i.

Dereferencing a variable means accessing the variable stored at a memory address:

取消引用变量意味着访问存储在内存地址的变量:

int i = 5;
int * p;
p = &i;
*p = 7; //*p returns the variable stored at the memory address stored in p, which is i.
//i is now 7

回答by nagaradderKantesh

find the below explanation:

找到以下解释:

int main()
{
    int a = 10;// say address of 'a' is 2000;
    int *p = &a; //it means 'p' is pointing[referencing] to 'a'. i.e p->2000
    int c = *p; //*p means dereferncing. it will give the content of the address pointed by 'p'. in this case 'p' is pointing to 2000[address of 'a' variable], content of 2000 is 10. so *p will give 10. 
}

conclusion :

结论 :

  1. &[address operator] is used for referencing.
  2. *[star operator] is used for de-referencing .
  1. &[地址运算符] 用于引用。
  2. *[星号运算符] 用于取消引用 .

回答by zaursh

Referencing

参考

&is the reference operator. It will refer the memory address to the pointer variable.

&是参考运算符。它将内存地址指向指针变量。

Example:

例子:

int *p;
int a=5;
p=&a; // Here Pointer variable p refers to the address of integer variable a.

Dereferencing

解除引用

Dereference operator *is used by the pointer variable to directly access the value of the variable instead of its memory address.

*指针变量使用解引用运算符直接访问变量的值而不是其内存地址。

Example:

例子:

int *p;
int a=5;
p=&a;
int value=*p; // Value variable will get the value of variable a that pointer variable p pointing to.

回答by Tyler Curtis Jowers

The context that * is in, confuses the meaning sometimes.

* 所在的上下文,有时会混淆意思。

  // when declaring a function
int function(int*); // This function is being declared as a function that takes in an 'address' that holds a number (so int*), it's asking for a 'reference', interchangeably called 'address'. When I 'call'(use) this function later, I better give it a variable-address! So instead of var, or q, or w, or p, I give it the address of var so &var, or &q, or &w, or &p.   

//even though the symbol ' * ' is typically used to mean 'dereferenced variable'(meaning: to use the value at the address of a variable)--despite it's common use, in this case, the symbol means a 'reference', again, in THIS context. (context here being the declaration of a 'prototype'.) 


    //when calling a function
int main(){ 
    function(&var);  // we are giving the function a 'reference', we are giving it an 'address'
  }

So, in the context of declaringa typesuch as int or char, we would use the dereferencer' * ' to actually mean the reference (the address), which makes it confusing if you see an error message from the compiler saying: 'expecting char*' which is asking for an address.

所以,在的情况下,宣布一个类型,如int或字符,我们将使用dereferencer“*”实际上是指参考(地址),这使得它混乱,如果你看到来自编译器说的错误消息:“期待char*' 要求一个地址。

In that case, when the * is after a type(int, char, etc.) the compiler is expecting a variable's address. We give it this by using a reference operator, alos called the address-ofoperator ' & ' before a variable. Even further, in the case I just made up above, the compiler is expecting the address to hold a character value, not a number. (type char * == address of a value that has a character)

在这种情况下,当 * 在类型(int、char 等)之后时,编译器需要一个变量的地址。我们通过使用引用运算符来实现这一点,也称为变量前的地址运算符“&”。更进一步,在我刚刚在上面编写的情况下,编译器期望地址保存一个字符值,而不是一个数字。(键入 char * == 具有字符的值的地址)

int* p;
int *a;   // both are 'pointer' declarations. We are telling the compiler that we will soon give these variables an address (with &).

int c = 10;  //declare and initialize a random variable
//assign the variable to a pointer, we do this so that we can modify the value of c from a different function regardless of the scope of that function (elaboration in a second)

p = c; //ERROR, we assigned a 'value' to this 'pointer'. We need to assign an 'address', a 'reference'.
p = &c; // instead of a value such as: 'q',5,'t', or 2.1 we gave the pointer an 'address', which we could actually print with printf(), and would be something like
//so
p = 0xab33d111; //the address of c, (not specifically this value for the address, it'll look like this though, with the 0x in the beggining, the computer treats these different from regular numbers)
*p = 10; // the value of c

a = &c; // I can still give c another pointer, even though it already has the pointer variable "p"

*a = 10;
 a = 0xab33d111;

Think of each variable as having a position (or an index value if you are familiar with arrays) and a value. It might take some getting used-to to think of each variable having two values to it, one value being it's position, physically stored with electricity in your computer, and a value representing whatever quantity or letter(s) the programmer wants to store.

将每个变量视为具有位置(或索引值,如果您熟悉数组)和值。可能需要一些时间来适应每个变量都有两个值,一个值是它的位置,物理存储在计算机中,一个值代表程序员想要存储的任何数量或字母。

//Why it's used
int function(b){
    b = b + 1; // we just want to add one to any variable that this function operates on.
} 

int main(){

    int c = 1;  // I want this variable to be 3.

    function(c); 
    function(c);// I call the function I made above twice, because I want c to be 3.

     // this will return c as 1. Even though I called it twice.
     // when you call a function it makes a copy of the variable.
     // so the function that I call "function", made a copy of c, and that function is only changing the "copy" of c, so it doesn't affect the original
}
  //let's redo this whole thing, and use pointers

int function(int* b){ // this time, the function is 'asking' (won't run without) for a variable that 'points' to a number-value (int). So it wants an integer pointer--an address that holds a number.
*b = *b + 1; //grab the value of the address, and add one to the value stored at that address
}

int main(){
    int c = 1; //again, I want this to be three at the end of the program
    int *p = &c; // on the left, I'm declaring a pointer, I'm telling the compiler that I'm about to have this letter point to an certain spot in my computer. Immediately after I used the assignment operator (the ' = ') to assign the address of c to this variable (pointer in this case) p. I do this using the address-of operator (referencer)' & '.
    function(p); // not *p, because that will dereference. which would give an integer, not an integer pointer ( function wants a reference to an int called int*, we aren't going to use *p because that will give the function an int instead of an address that stores an int.

    function(&c); // this is giving the same thing as above, p = the address of c, so we can pass the 'pointer' or we can pass the 'address' that the pointer(variable) is 'pointing','referencing' to. Which is &c. 0xaabbcc1122...


      //now, the function is making a copy of c's address, but it doesn't matter if it's a copy or not, because it's going to point the computer to the exact same spot (hence, The Address), and it will be changed for main's version of c as well.

}

Inside each and every block, it copies the variables (if any) that are passed into (via parameters within "()"s). Within those blocks, the changes to a variable are made to a copyof that variable, the variable uses the same letters but is at a different address (from the original). By using the address "reference" of the original, we can change a variable using a block outside of main, or inside a child of main.

在每个块内,它复制传入的变量(如果有)(通过“()”中的参数)。在这些块中,对变量的更改是对该变量的副本进行的,该变量使用相同的字母但位于不同的地址(与原始地址不同)。通过使用原始地址“引用”,我们可以使用 main 外部的块或 main 的子代内部的块来更改变量。