是否可以交换两个变量的地址? [英] Is it possible to swap the addresses of two variables?

查看:112
本文介绍了是否可以交换两个变量的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以像这样交换两个变量的值

I know that it is possible to swap the value of two variables like so

#include <stdio.h>

void swap(int *x, int *y);
int main(){
    int x=5,y=10;
    swap(&x, &y);
    printf("x: %d, y: %d\n", x, y);
    return 0;
}

void swap(int *x, int *y){ 
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}

但是可以交换这两个变量的地址吗?而且我的意思不是仅仅指向它们的指针,然后交换指针持有的值,而是交换它们,所以在交换函数之后,x的地址现在是交换函数之前的y的地址,反之亦然.

But is it possible to swap the addresses of those two variables? And I don't mean just making a pointer to them and then swapping the values held by the pointers, I mean actually swapping the two, so that after the swap function the address of x is now the address of y before the swap function and vice versa.

如果这是一个愚蠢的问题,我深表歉意,但我很好奇,看看是否有可能这样做.如果无法执行此操作,为什么?

I apologise if this is a silly question but I'm curious to see if such a thing is even possible. If this behaviour is not possible, why?

推荐答案

否,这是不可能的.您无法选择变量的地址,也无法对其进行修改.

No, that's not possible. You don't get to choose the addresses of variables and you can't modify them either.

这篇关于是否可以交换两个变量的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆