hi, 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 14 5446
<pr*********@gmail.com> wrote: 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.
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> wrote 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
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 wrote: <pr*********@gmail.com> wrote
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
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
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 ---
osmium wrote: 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.
Perhaps, this will qualify as a "cute" digression. But nonetheless, the
following code is more efficient in terms of both space and time:
void
swap (int i, int j)
{
i = i ^ j ;
j = i ^ j ;
i = i ^ j ;
}
> Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
void swap (int i, int j) { i = i ^ j ; j = i ^ j ; i = i ^ j ; }
Oops, the function should be replaced by a macro (since all parameters
are call-by-value in C):
something like:
#define swap(i,j) do { i=i^j; j=i^j; i=i^j; } while (0)
In article <11*********************@g44g2000cwa.googlegroups. com>,
<pr*********@gmail.com> wrote: 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.
Are i and j the same type? Are they the same size? Is it certain
that the location of i is suitably aligned for a value of j's type
(and vice versas) ?
--
Any sufficiently old bug becomes a feature.
Sorav Bansal wrote on 23/07/05 : Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
void swap (int i, int j) { i = i ^ j ; j = i ^ j ; i = i ^ j ; }
Oops, the function should be replaced by a macro (since all parameters are call-by-value in C): something like:
#define swap(i,j) do { i=i^j; j=i^j; i=i^j; } while (0)
Lack of parenthesis around the parameters, multiple evaluation of the
parameters, non portable code, you are cooked ! (^ works portably on
unsigned integer types)
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Sorav Bansal <sb*****@stanford.edu> writes: Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
void swap (int i, int j) { i = i ^ j ; j = i ^ j ; i = i ^ j ; }
Please read the FAQ.
20.15c: How can I swap two values without using a temporary?
A: The standard hoary old assembly language programmer's trick is:
a ^= b;
b ^= a;
a ^= b;
But this sort of code has little place in modern, HLL
programming. Temporary variables are essentially free,
and the idiomatic code using three assignments, namely
int t = a;
a = b;
b = t;
is not only clearer to the human reader, it is more likely to be
recognized by the compiler and turned into the most-efficient
code (e.g. using a swap instruction, if available). The latter
code is obviously also amenable to use with pointers and
floating-point values, unlike the XOR trick. See also questions
3.3b and 10.3.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Sorav Bansal wrote: Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
void swap (int i, int j) { i = i ^ j ; j = i ^ j ; i = i ^ j ; }
Oops, the function should be replaced by a macro (since all parameters are call-by-value in C): something like:
#define swap(i,j) do { i=i^j; j=i^j; i=i^j; } while (0)
It is a herd of errors waiting to trap you. Try calling it with:
swap(a[i], a[j]);
oh, you are going to make the passed items pointers. Alright, then
try the same swap when i == j and see what you get. Read the FAQ.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address! pr*********@gmail.com writes: hi, 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.
Several people have offered possible solutions, but I'm not sure that
anybody understands what you're asking. I know I don't.
A variable's address is determined when the variable is created, and
doesn't change over the lifetime of the variable. Changing the
address of a variable doesn't even make sense; if it has a different
address, it's a different variable.
Just to add to the confusion, you tell us that the addresses are 2000
and 3000. It's important to keep in mind that addresses are not
integers (though they might sometimes be represented that way).
The closest thing I can think of to what you're asking for is to
declare two variables, then declare two pointers that point to them.
You can then swap the values of the pointer variables. Swapping two
variables is (or should be) very straightforward, whether they're
pointers or anything else; others have posted examples using a
temporary variable.
If you can clarify what you're asking for, perhaps we can help.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote: pr*********@gmail.com writes: hi, 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.
Nice Cajun-speak there. Well done, sir.
Is it the normal swapping of two number ? if no please help me out.
Several people have offered possible solutions, but I'm not sure that anybody understands what you're asking. I know I don't.
I think it is perfectly clear what he is asking for.
And that the answer is, as it is to most questions asked here, that it
can't be done in standard C.
On 2005-07-23 13:07:21 -0400, Sorav Bansal <sb*****@stanford.edu> said: osmium wrote:
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.
Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
On almost every single processor that I've ever worked on, that is
simply false. Additionally, it will give the wrong result when both
variables have the same value.
Put simply, don't do it.
--
Clark S. Cox, III cl*******@gmail.com
Clark S. Cox III wrote: On 2005-07-23 13:07:21 -0400, Sorav Bansal <sb*****@stanford.edu> said: Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
<added code we're talking about>
i ^= j;
j ^= i;
i ^= j; On almost every single processor that I've ever worked on, that is simply false. Additionally, it will give the wrong result when both variables have the same value.
Wrong. It still works if both variables have the same value. See the
breakdown just below:
int a = SOME_VALUE_WE_DONT_CARE_ABOUT;
int i = a;
int j = a;
i ^= j; // Effectively i = i ^ j = a ^a = 0. So after this step i = 0,
j = a;
j ^= i; // j = j ^ i = a ^ 0 = a; After this step i = 0, j = a;
i ^= j; // i = i ^ j = 0 ^ a = a; Finally i = a, j = a;
As you can see it still works if both variables have the same value.
The problem arises when the two variables are the same variable, as in
the following code:
void swap (int *i, int *j) {
*i ^= *j;
*j ^= *i;
*i ^= *j;
}
int main (int argc, char **argv) {
int a = SOME_VALUE_WE_DONT_CARE_ABOUT;
swap (&a, &a);
printf("%d\n", a);
}
In this case in the swap function we have:
*i ^= *j; // ==> a ^= a ==> a = a ^ a = 0;
*j ^= *i; // ==> a ^= a ==> a = a ^ a = 0 ^ 0 = 0;
*i ^= *j; // ==> a ^= a ==> a = a ^ a = 0 ^ 0 = 0;
So we end up with a 0 in a, not the initial value as was intended. Put simply, don't do it.
Agreed.
Sorav Bansal wrote: osmium wrote:
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.
Perhaps, this will qualify as a "cute" digression. But nonetheless, the following code is more efficient in terms of both space and time:
void swap (int i, int j) { i = i ^ j ; j = i ^ j ; i = i ^ j ; }
It certainly is, since it can be optimised to
void swap( int i, int j ) {}
and this is futher subject to cheap inlining.
However, it's semantics are a tad unlike any kind of swap function.
--
Chris "electric hedgehog" Dollin
predicting self-predictors' predictions is predictably unpredictable. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doug Baroter |
last post by:
What is a good method/mechanism to swap the position of multiple
columns?
For instance, tblXZY has the followings columns and respective
positions:
tblXZY
======
xyzUUID 1
fn 2
ln 3...
|
by: ma740988 |
last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more
specifically item 56. There's a statement:
"Prefer to provide a nonmember swap function in the same namespace as
your type when...
|
by: Kai-Uwe Bux |
last post by:
Hi folks,
I am still struggling with the rules for name lookup.
Please consider:
namespace xxx {
|
by: Jongmin Lee |
last post by:
Hi Everybody,
I have very simple code snippet to explain my problem.
Class "Swap" is construncted in "Main" with two initial
variables.
Later, "Swap" class is going to swap those two...
|
by: Niels Dekker (no reply address) |
last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd
Edition, by Scott Meyers), what swap is chosen to be called?
using std::swap;
swap(a, b);
Suppose there is a global ::swap...
|
by: ma740988 |
last post by:
Consider:
# include <vector>
# include <iostream>
# include <cstdlib>
# include <ctime>
bool
ispow2i ( double n )
{
|
by: imnilima |
last post by:
hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
|
by: Jess |
last post by:
Hello,
It is said that if I implement a "swap" member function, then it
should never throw any exception. However, if I implement "swap" non-
member function, then the restriction doesn't...
|
by: George2 |
last post by:
Hello everyone,
The following swap technique is used to make assignment operator
exception safe (means even if there is exception, the current object
instance's state is invariant).
It used...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |