使用 C 通过引用传递
据我了解,当您通过 C 中的函数通过引用传递时,该函数的参数将采用将要修改的指针的地址。我非常困惑为什么这个通过引用传递的例子不起作用。任何人都可以指出我正确的方向....
这应该输出一个交换,但是当我编译时,交换没有发生,为什么这个按引用传递不起作用?
#include <stdio.h>
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
getchar();
getchar();
return 0;
}
I understand that when you pass by reference through a function in C, the parameters of the function take in the address of the pointer that will be modified. I am extremley boggled on why this example of pass by reference is not working. Can anyone point me in the right direction....
This should output a swap but when i compile the swap does not occur why is this pass by reference not working?
#include <stdio.h>
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
getchar();
getchar();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你想要:
You want:
在
swapnum
中,您正在交换指针(也是 int 值)。试试这个:In
swapnum
, you are swapping the pointers (which are also int values). Try this instead:您对 swapnum() 的实现有点不正确。该函数有 2 个 (int *) 参数。即,i和j是存储a和b的引用的整数指针。当你执行 int temp = i; 时您实际上是将一个指针分配给整数变量(这是不正确的),然后代码片段不是交换值,而是使用地址。这就是你需要的
Your implementation of swapnum() is a bit improper. The function takes 2 (int *) parameters. That is, i and j are integer pointers storing references of a and b. when you do int temp = i; you are actually assigning a pointer to integer variable (which is incorrect) and then instead of swapping the values, the code snippet plays around with addresses. This is what you need
您忘记取消引用函数内的指针。因此,您最终会重新分配本地指针值,而不是更改所指向的实际值,并且它没有任何效果。
因此,使用
*
解引用运算符:You forgot to dereference your pointers inside the function. Thus you end up reassigning the local pointer values rather than changing the actual value being pointed to and it has no effect.
So, use the
*
dereference operator:我想知道为什么你没有收到任何编译器警告/错误。您需要取消引用函数中的引用:
原因是当函数调用时,
swapnum()
中的i
和j
是原始变量的地址。叫。因此,当您仅使用i
或j
时,您将获得变量的地址,而不是内容。下面是正在发生的事情的一个想法:然后,在
swapnum(int *i, int *j)
中:I'm wondering why you didn't get any compiler warning/error. You need to dereference your reference in the function:
The reason is
i
andj
insideswapnum()
are addresses to original variables when the function is called. So when you use onlyi
orj
, you're getting the address of the variable, not the content. Here is an idea of what is going on:Then, inside
swapnum(int *i, int *j)
:在
swapnum
函数中,您仅分配该函数本地的变量i
和j
。这不会对该函数之外产生任何影响。你应该尝试:In the
swapnum
function, you are only assigning the variablesi
andj
which are local to this function. This won't have any effect outside of this function. You should try:它(希望)崩溃,因为在这个函数中:
i
和j
是指针,而temp
是一个int。不能将指针分配给 int。如果您想交换i
和j
中的值,请执行以下操作:It's (hopefully) crashing because in this function:
i
andj
are pointers, andtemp
is an int. You cannot assign a pointer to an int. If you want to swap the values ini
andj
do this: