473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

swap of two no.?????

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

Nov 7 '06 #1
10 1715
Cla
im******@gmail.com wrote:
hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
Instead, I always wanted to know more about history,
but I just cannot remember the dates.
Oh, well.
Nov 7 '06 #2

im******@gmail.com wrote:
hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
What you want is so wrong that it can't be answered.

Perhaps you aren't explaining your need correctly. Could you please
expand on what it is that you want?

Nov 7 '06 #3
In article <11**********************@m73g2000cwd.googlegroups .com>,
im******@gmail.com <im******@gmail.comwrote:
>i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
Could you clarify what you mean? If your pointers are I and F to
start, then afterwards you want I to point to the float and F to
point to the int -- a pointer operation? Or you want the memory location
that used to hold the int to now hold the float and vice versa?
If so, with or without I and F being updated to point to the new locations?

If you are trying to change the pointers without moving the memory,
then you have the problem that the pointers would point to the wrong
types afterwards -- you cannot use an int pointer to point to a float.

If you are trying ot change the memory locations, then you have
the problem that int and float may be different sizes, so when you
move one of them there might not be enough room at the destination.

It is not uncommon for floats to have to be aligned on addresses that
are internally multiples of 4 bytes, but for int to only be restricted
to multiple of 2 bytes, so you can run into alignment problems even if
everything else worked out.

--
Prototypes are supertypes of their clones. -- maplesoft
Nov 7 '06 #4
im******@gmail.com:
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

void SwapIntWithFloat(int *const pi,float *const pf)
{
int const temp i = *pi;
*pi = *pf;
*pf = i;
}
You might want some safe-guard such as:

assert(pi); assert(pf);

assert(*pf>=INT_MIN && *pf<=INT_MAX);

--

Frederick Gotham
Nov 7 '06 #5
im******@gmail.com wrote:
hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

void perverse_swap(int *i, float *f)
{
int t;
t = *f; /* if you want f rounded instead of truncated, you know
what to do */
*f = *i;
*i = t;
/* error detection & handling left as an exercise */
}
Nov 7 '06 #6

im******@gmail.com wrote:
hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
My mom told me that

a) "Hey" is not a polite way to greet people, especially when you don't
know them well
b) "I want" never gets
c) Capital letters are free

Nov 8 '06 #7
"im******@gmail.com" <im******@gmail.comwrote:
hey
Ho-nonny-nonny.
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
Don't listen to the others. Your teacher obviously doesn't want you to
swap values, because that can easily be done with a single temp object,
no need for pointers at all. What he wants is that you swap the _bytes_
representing these values. Like this:

assert(sizeof (int) == sizeof (float));
{
unsigned char temp[sizeof (int)];
memcpy(temp, the_int, sizeof (int));
memcpy(the_int, the_float, sizeof (int));
memcpy(the_float, temp, sizeof (int));
}

Make sure you use the float as soon as possible, so that you'll be told
if the int didn't contain a valid float representation.

Richard
Nov 9 '06 #8
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"im******@gmail.com" <im******@gmail.comwrote:
>hey

Ho-nonny-nonny.
>i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

Don't listen to the others. Your teacher obviously doesn't want you to
swap values, because that can easily be done with a single temp object,
no need for pointers at all. What he wants is that you swap the _bytes_
representing these values.
[...]

It's not at all obvious *what* this teacher wants him to do. Neither
swapping the values nor swapping the representations makes much sense,
though both are possible (assuming no range errors if we're swapping
values, and the same size if we're swapping representations).

I think the only appropriate response is "Huh?". Or rather, we need
to know *exactly* what's required.

--
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.
Nov 10 '06 #9
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"im******@gmail.com" <im******@gmail.comwrote:
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
Don't listen to the others. Your teacher obviously doesn't want you to
swap values, because that can easily be done with a single temp object,
no need for pointers at all. What he wants is that you swap the _bytes_
representing these values.
It's not at all obvious *what* this teacher wants him to do. Neither
swapping the values nor swapping the representations makes much sense,
Nevertheless, I think it would be better for him in the end if he handed
in my solution, as-is.

Richard
Nov 10 '06 #10
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
[...]
>It's not at all obvious *what* this teacher wants him to do. Neither
swapping the values nor swapping the representations makes much sense,

Nevertheless, I think it would be better for him in the end if he handed
in my solution, as-is.
Ah, I see your point.

--
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.
Nov 10 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
2
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...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I am still struggling with the rules for name lookup. Please consider: namespace xxx {
14
by: Otto Meijer | last post by:
Hi everyone, for one of my projects I need to figure out the size of the swap file(s) of a certain system. The problem is I need to do this on a host of platforms like: HP_UX, Solaris, Linux,...
9
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...
4
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...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
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...
4
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...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.