#include <stdio.h>
void swap( char* pA, char* pB, unsigned tam) {
for( unsigned i = 0; i < tam; i++) {
char tmp = *pA;
*pA = *pB;
*pB = tmp;
pA = pA + 1;
pB = pB + 1;
}
}
int main(int argc, char** argv) {
double a = 1.0;
double b = 2.0;
printf("linea: %d - antes a(%f) b(%f)\n", __LINE__, a, b);
swap(&a, &b, sizeof(double)); //This line gives the error
printf("linea: %d - despues a(%f) b(%f)\n", __LINE__, a, b);
}
When I call the swap method I get the error message in the title, any idea why? I saw many other posts with solutions but none of them fixed this problem
Well, your function is expecting a pointer to a character for the first two arguments
but you are passing in pointers to double
The following would allow you to swap two doubles, unless there is a specific reason you are swapping one byte at a time:
There are a number of ways of writing a generic swap function. If it will be used for only one type (so it doesn't need to be generic), the size argument is not needed and you can pass pointers of the relevant type (
double *
in the question) and swap using indirection.There might well be advantages to making that into:
This could be placed in a header and used safely.
If it will be used for multiple types, you should use
void *
in the function arguments (andsize_t
for the size of the type).You can use
memcpy()
if you don't mind living dangerously (you should probably addrestrict
to thev1
andv2
pointer types if you do that). Again, the function could probably be madestatic inline
to good effect — that also applies to the other implementations below.If you don't like the idea of large objects being allocated on the stack, you can copy the data in chunks, but you have to work quite a bit harder.
Notwithstanding anything that GCC permits, the C standard says you can't increment a
void *
because there is no known size to increment it by. That's why the pointers are converted tounsigned char *
. Clearly, you can tune the chunk size to suit your system. Any power of 2 in the range 16..1024 is probably usable, and other values than a power of 2 can be used if you prefer.If you don't mind the overhead, you can dynamically allocate a buffer:
If memory allocation fails, the swap doesn't occur. That's bad, so you might fall back on the 'fixed size buffer and swap in chunks', but that is probably quicker than this anyway.
I would use Implementation 2 in preference to Implementation 3; dynamic memory allocation is expensive. I would probably use Implementation 2 in preference to Implementation 1 as the extra cost of the looping is minimal and using a fixed amount of stack works well. I have not benchmarked any of these to verify my assertions. (If you're swapping megabyte sized lumps of data, you should probably think again — use pointers instead. If you're only swapping smaller chunks, Implementation 1 is simple and safe.)
With any of the generic swap implementations, your main program becomes:
I'm assuming that at least the declaration of
generic_swap()
is available before the start ofmain()
.Note that using either
double_swap()
orgeneric_swap()
means that no casts are necessary in the code. Minimizing casts is A Good Idea™.See also Universal array element swap in C.
I get the error message in the title ? Because you are passing double variable address and catching with
char*
so compiler is sayingSo you have two ways either
typecast
the address of double variable aschar*
or catch withdouble*
itself.Case 1:
Case 2 :- Second way is typecast the address of double variable as
char*
send it toswap()