sách gpt4 ăn đã đi

c - 结构示例 *e : differences between function(&e) and function(e)

In lại 作者:太空狗 更新时间:2023-10-29 15:55:04 28 4
mua khóa gpt4 giày nike

如果我有一个struct example *efunction(&e)function(e) 之间有什么区别?

一个例子。

这是第一个代码:

#include 

struct example
{
số nguyên x;
int y;
};

void function (struct example **);

int chính ()
{
struct example *e;

function (&e);

trả về 0;
}

void function (struct example **e)
{
/ * ... */
}

这是第二个代码:

#include 

struct example
{
số nguyên x;
int y;
};

void function (struct example *);

int chính ()
{
struct example *e;

function (e);

trả về 0;
}

void function (struct example *e)
{
/ * ... */
}

这两个代码有什么区别?谢谢!

câu trả lời hay nhất

hiện hữu第一个 中,您将指针的地址传递给结构。在第二个中,您传递结构的地址。

在这两种情况下,chức năng 都可以更改您传递给它的结构:

(*e)->x = 10; // First, needs additional dereferencing *.

e->x = 10; // Second.

首先,你也可以给 chủ yếu() của 一个不同的值,例如将另一个结构的地址分配给它,或者将它设置为:

*e = NULL;

您实际上忘记了第三种情况:

function(struct example e) { ... }

此处函数获取您传递给它的结构的副本。

关于c - 结构示例 *e : differences between function(&e) and function(e),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166160/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress