交换地址? [英] swap addresses ?
问题描述
请帮我解决这个问题: -
如何交换两个地址。例如变量i位于2000和j
at 3000。现在我调用swap函数。结果应该是我应该是
现在有地址3000而j应该有2000.
这是两个数字的正常交换吗?如果没有,请帮助我。
提前致谢
please help me with this problem : -
how to swap two addresses .For eg variable i is located at 2000 and j
at 3000 . Now i call swap function . Result should be that i should be
now having address 3000 and j should be having 2000.
Is it the normal swapping of two number ? if no please help me out.
Thanks in advance
推荐答案
< pr *********@gmail.com>写道:
<pr*********@gmail.com> wrote:
如何交换两个地址。例如变量i位于2000和j
3000。现在我调用swap函数。结果应该是我现在应该有地址3000而j应该有2000.
这是两个号码的正常交换吗?如果没有,请帮帮我。
how to swap two addresses .For eg variable i is located at 2000 and j
at 3000 . Now i call swap function . Result should be that i should be
now having address 3000 and j should be having 2000.
Is it the normal swapping of two number ? if no please help me out.
这样的东西:
int i,j,temp;
/ *给i和j * /
temp = i;
i = j;
j = temp;
这是*正确的*方式。像这样的问题可能会引发一些所谓的可爱的问题。离题。
请注意,我们没有交换地址,我们交换了变量。我跟着
你的描述,而不是你的留言标题。 2000和3000的数字与
无关 - 高级程序员不应该知道
的地址数。他们处理变量*名称*。
Something like this:
int i, j,temp;
/* give values to i and j*/
temp = i;
i = j;
j = temp;
That is the *right* way to do it. A question such as this may invoke some
so called "cute" digressions.
Note that we didn''t swap the address, we swapped the variables. I followed
your description, not your message title. The numbers 2000 and 3000 have
nothing to do with it - high level programmers are not supposed to know the
numbers of addresses. They deal in variable *names*.
< pr ********* @ gmail.com>写了
<pr*********@gmail.com> wrote
如何交换两个地址。例如,变量i位于2000,j
位于3000。现在我调用swap函数。结果应该是我现在应该有地址3000而j应该有2000.
这是两个号码的正常交换吗?如果没有,请帮助我。
提前致谢
how to swap two addresses .For eg variable i is located at 2000 and j
at 3000 . Now i call swap function . Result should be that i should be
now having address 3000 and j should be having 2000.
Is it the normal swapping of two number ? if no please help me out.
Thanks in advance
几乎。
如果你使用标识符i和j对于C中的变量,你没有
控制地址。
temp = i;
i = j;
j = temp;
将交换它们
如果你有指向变量的指针,那么ptr2k保存地址
" 2000"并且ptr3k在您的平台相关的
机器表示中保存地址3000,然后您可以执行此操作。
int temp = * ptr2k;
* ptr2k = * ptr3k;
* ptr3k = temp;
这将交换这些地址的内容
Nearly.
If you use the identifiers "i" and "j" for your variables in C you have no
control over the address.
temp = i;
i = j;
j = temp;
will swap them
If you have pointers to the variables, such that ptr2k holds the address
"2000" and ptr3k holds the address "3000", in your platform-dependent
machine representation, then you can do this.
int temp = *ptr2k;
*ptr2k = *ptr3k;
*ptr3k = temp;
That will swap the contents of those addresss
Malcolm写道:
Malcolm wrote:
< pr ********* @ gmail.com>写了
<pr*********@gmail.com> wrote
如何交换两个地址。例如,变量i位于2000,j
位于3000。现在我调用swap函数。结果应该是我现在应该有地址3000而j应该有2000.
这是两个号码的正常交换吗?如果没有,请帮助我。
提前致谢
how to swap two addresses .For eg variable i is located at 2000 and j
at 3000 . Now i call swap function . Result should be that i should be
now having address 3000 and j should be having 2000.
Is it the normal swapping of two number ? if no please help me out.
Thanks in advance
几乎。
如果您使用标识符i和j对于C中的变量,你无法控制地址。
temp = i;
i = j;
j = temp;
<将交换它们
如果你有指向变量的指针,那么ptr2k保存地址
2000。并且ptr3k在您的平台相关的机器表示中保存地址3000,然后您可以执行此操作。
int temp = * ptr2k;
* ptr2k = * ptr3k;
* ptr3k = temp;
那将交换这些地址的内容
Nearly.
If you use the identifiers "i" and "j" for your variables in C you have no
control over the address.
temp = i;
i = j;
j = temp;
will swap them
If you have pointers to the variables, such that ptr2k holds the address
"2000" and ptr3k holds the address "3000", in your platform-dependent
machine representation, then you can do this.
int temp = *ptr2k;
*ptr2k = *ptr3k;
*ptr3k = temp;
That will swap the contents of those addresss
如果你真的想要交换地址..
void * temp = ptr2k;
ptr2k = ptr3k;
ptr3k = temp;
-
Joe Wright
所有东西都应尽可能简单,但不能简单。
- - 阿尔伯特爱因斯坦---
And if you really want to swap addresses..
void *temp = ptr2k;
ptr2k = ptr3k;
ptr3k = temp;
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
这篇关于交换地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!