特定参数传递样式:Call-By-Value、Call-By-Name 等

发布于 2024-10-06 05:38:41 字数 852 浏览 3 评论 0原文

正在准备期末考试,并遇到了这个过去的考试问题:

考虑以下用类 C 表示法编写的程序:

int i = 1;
A[] = {4, 0, 1, 2};

void mystery05(int from, int to)
{
    int temp;
    temp = A[from];
    A[from] = A[to];
    A[to] = temp;
    i = i + 2;
    to = -1;
}

int main(void)
{
    mystery05(A[i+2], A[i]);
}

在下表中,在 main 中调用 mymy05 后,在框中填写适当的变量值。每行对应一个特定的参数传递样式(即使用列出的样式而不是默认的 C 语言语义)。假设数组从 0 开始索引。

style               |___i___|__A[0]__|__A[1]__|__A[2]__|__A[3]__| 
call-by-value       |_______|________|________|________|________|
call-by-name        |_______|________|________|________|________|
call-by-reference   |_______|________|________|________|________|
call-by-value-result|_______|________|________|________|________|

我不确定如何解决这个问题,但如果是常规 C 语义,我认为答案将是

i = 3; A[0] = 4; A[1] = 2; A[2] = 1; A[3] = 0

Studying for my final exam, and came across this past exam question:

Consider the following program written in a C-like notation:

int i = 1;
A[] = {4, 0, 1, 2};

void mystery05(int from, int to)
{
    int temp;
    temp = A[from];
    A[from] = A[to];
    A[to] = temp;
    i = i + 2;
    to = -1;
}

int main(void)
{
    mystery05(A[i+2], A[i]);
}

In the table below, fill in the boxes with the appropriate variable values after the call to mystery05 in main. Each row corresponds to a specific parameter-passing style (ie. use the style listed instead of the default C-language semantics). Assume arrays are indexed from 0.

style               |___i___|__A[0]__|__A[1]__|__A[2]__|__A[3]__| 
call-by-value       |_______|________|________|________|________|
call-by-name        |_______|________|________|________|________|
call-by-reference   |_______|________|________|________|________|
call-by-value-result|_______|________|________|________|________|

I'm not sure on how to go about this, but if it was regular C semantics, I supposed the answers would be

i = 3; A[0] = 4; A[1] = 2; A[2] = 1; A[3] = 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-10-13 05:38:41

@S.洛特:
我认为指向字符串和数组的指针是通过引用调用的。我错了吗?

我同意:不想做所有的问题。如果他有考试,他应该会更清楚。
我想回答第一行,只是为了看看我是否理解正确。
所以我可能是错的!

按值调用:除非变量是全局变量,否则不会更改值
在这种情况下,它们必须是;否则 proc 如何利用 i.

i 和 A 数组都是全局的。

过程中发生的事情会改变值。

i 从值 1 开始,因此 A[3] 和 A[1] 的值交换。

A[3] 现在为 0 , A[1] 现在为 2 。 A[0] 和 A[2] 不变。

最后我的值变成了 3

我认为考试 q 错过了一个技巧,因为在 proc 调用之后没有询问 'to' 的值。

@S.Lott :
I thought 'pointer's to strings and arrays are call by reference. Am I wrong?

I agree: don't want to do all the question. If he has an exam he ought to be more clued up.
I would like to answer the first line though just to see if i have understood correctly.
So I could be wrong!

Call by value: doesn't change the values unless the variables are global
and in this case they have to be; for how otherwise can the proc make use of i.

Both i and the A array are global.

What happens in the proc changes the values.

i begins with value 1 so values of A[3] and A[1] swapped.

A[3] now 0 , A[1] now 2 . A[0] and A[2] unchanged.

finally i value changed to 3

I think the exam q missed a trick by not asking about the value of 'to' after the proc call.

つ低調成傷 2024-10-13 05:38:41

我认为按值调用是“常规 C 语义”的意思,

按名称调用是 C 所没有的。查一下。这不是“常规 C 语义”,

按引用调用假定所有参数都有“&”并且参数带有“*”。这不是“常规 C 语义”,而是一种不同的语义,但很容易在 C 中构建。

按值结果调用是 C 所没有的。查一下。

每个都是不同的。不要假设 C。不要像 C 一样阅读代码。你必须以不同的方式阅读代码,假设不同的事情。

call-by-value is -- I think -- what you mean by "regular C semantics"

call-by-name is something C doesn't have. Look it up. That's not "regular C semantics"

call-by-reference assumes that all the arguments have "&" and the parameters have "*". That's not "regular C semantics" that's a different semantics, but easily built in C.

call-by-value-result is something C doesn't have. Look it up.

Each is different. Don't assume C. Don't read the code as if it was C. You have to read the code in different ways assuming different things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文