gpt4 book ai didi

c - 为什么我们要引用变量地址?它的优点是什么?

转载 作者:行者123 更新时间:2023-12-02 01:43:20 25 4
gpt4 key购买 nike

我尝试在谷歌上搜索答案,但没有成功。我的编程老师只是提到它是一种可以使用的可能性,但我看不到用例,我想我理解它是如何工作的,所以我的理论是引用 &var 比使用 var 更快。如果是这样的话,难道只是为了这个目的吗?我有什么遗漏的吗?我完全错了吗?编辑:抱歉,我的意思是 - 通过使用变量的地址引用变量比仅使用变量更快

最佳答案

为其他软件提供变量引用:

double x0, y0;
double x1, y1;
double x2, y2;

/* Convert polar coordinates to rectangular coordinates.
This code needs two results from each call, but a function can only
return one thing. And we do not want the function to set the same
variables each time, so we cannot use fixed global variables. In
each call, we pass the function pointers to two variables we want
it to put the results in.
*/
ConvertPolarToRectangular(&x0, &y0, 3, .17);
ConvertPolarToRectangular(&x1, &y1, 4, .23);
ConvertPolarToRectangular(&x2, &y2, 5, -.03);

当我们编译程序时不知道需要多少数据时,可以使用数据:

//  Get number of items needed.
scanf("%d", &N);

/* Allocate memory for records. This makes Records point to enough
memory for N records (if the malloc succeeds).
*/
struct Record *Records = malloc(N * sizeof *Records);

创建链表、树和其他相互连接的数据结构:

struct ListItem
{
struct ListItem *Next;
int Data;
}

struct ListItem *FirstItem = malloc(sizeof *FirstItem);
struct ListItem *SecondItem = malloc(sizeof *SecondItem);
FirstItem->Next = SecondItem;
SecondItem->Next = NULL;

/* Now we can keep allocating memory for as many items as we need
and keep linking them together into a list. When we want to
delete one item, we can take it out of the chain without moving
the other items in memory:
*/
FirstItem->Next = ThirdItem;
free(SecondItem);

关于c - 为什么我们要引用变量地址?它的优点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71253325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com