c - C中“引用”和“取消引用”的含义

365bet赌城 ⌚ 2025-08-02 18:06:41 👤 admin 👁️ 6734 ❤️ 421
c - C中“引用”和“取消引用”的含义

* 所在的上下文有时会混淆含义。

// 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'

}

因此,在声明int 或 char 等类型的上下文中,我们将使用解引用器 ' * ' 来实际表示引用(地址),如果您看到来自编译器的错误消息说:'expecting char*' 要求地址。

在这种情况下,当 * 位于类型(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;

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

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

}

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

相关数据

浙江肾内科医院口碑排行榜

浙江肾内科医院口碑排行榜

浙江肾内科医院排名前列有浙江大学医学院附属第一医院,浙江省人民医院,温州医科大学附属第二医院,浙江大学医学院附属邵逸夫医院,温

07-09 365bet现场走地盘
韩国队阵容大名单(2022世界杯最新)

韩国队阵容大名单(2022世界杯最新)

韩国队公布世界杯26人名单,此前脸部受伤的孙兴慜带伤入选,中超球队山东泰山外援孙准浩在列。 韩国队世界杯阵容如下: 人数:26人,平均

06-28 365bet现场走地盘
虫加圣是什么字,怎么读?

虫加圣是什么字,怎么读?

题目 举报虫加圣是什么字,怎么读?扫码下载作业帮搜索答疑一搜即得 答案解析 查看更多优质解析解答一举报这东西挺好吃的蛏chēng部首笔画部

07-02 best365中国官网
如何将web项目发布至tomcat

如何将web项目发布至tomcat

如何将Web项目发布至Tomcat? 要将Web项目发布至Tomcat,需要准备Tomcat环境、打包项目为WAR文件、将WAR文件部署到Tomcat、配置Tomcat服务器、启动Tomcat

07-25 365bet赌城
做指甲可以撐多久?5分鐘帶你了解美甲維持時間︱台北天母美甲推薦︱JR美學苑

做指甲可以撐多久?5分鐘帶你了解美甲維持時間︱台北天母美甲推薦︱JR美學苑

你有沒有做指甲的習慣呢?你是不是也想問美甲可以維持多久呢?擔心美甲做完很快就掉漆了嗎?其實美甲所使用的凝膠與一般指甲油是不一樣

07-01 best365中国官网
手到擒来的意思

手到擒来的意思

来 [ lái ] 1. 由另一方面到这一方面,与“往”、“去”相对。 如 来回。来往。过来。归来。来鸿去燕(喻来回迁徙,不能在一地常住;亦喻书

07-13 365bet现场走地盘