Ian said:
Army1987 said:
Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
How 'bout:
int swap (void *a, void *b)
{
void *temp;
if (sizeof *a != sizeof *b) {
Have you tried to compile this?
Or this?
One doubts it.
"/tmp/foo.c", line 4: error: operand of "sizeof" cannot have incomplete type
if (sizeof *a != sizeof *b) {
^
"/tmp/foo.c", line 4: error: operand of "sizeof" cannot have incomplete type
if (sizeof *a != sizeof *b) {
^
"/tmp/foo.c", line 5: error: identifier "fputs" is not defined
fputs("Attempt to swap variables of incompatible types", stderr);
^
"/tmp/foo.c", line 5: error: identifier "stderr" is not defined
fputs("Attempt to swap variables of incompatible types", stderr);
^
"/tmp/foo.c", line 8: error: identifier "malloc" is not defined
temp = malloc(sizeof *a);
^
"/tmp/foo.c", line 8: error: operand of "sizeof" cannot have incomplete type
temp = malloc(sizeof *a);
^
"/tmp/foo.c", line 9: error: identifier "NULL" is not defined
if (temp == NULL) {
^
"/tmp/foo.c", line 10: error: identifier "perror" is not defined
perror("Unable to allocate memory");
^
"/tmp/foo.c", line 13: error: identifier "memcpy" is not defined
memcpy(temp, a, sizeof *a);
^
"/tmp/foo.c", line 13: error: operand of "sizeof" cannot have incomplete
type
memcpy(temp, a, sizeof *a);
^
"/tmp/foo.c", line 14: error: identifier "memcpy" is not defined
memcpy(a, b, sizeof *a);
^
"/tmp/foo.c", line 14: error: operand of "sizeof" cannot have incomplete
type
memcpy(a, b, sizeof *a);
^
"/tmp/foo.c", line 15: error: identifier "memcpy" is not defined
memcpy(b, temp, sizeof *a);
^
"/tmp/foo.c", line 15: error: operand of "sizeof" cannot have incomplete
type
memcpy(b, temp, sizeof *a);
^
"/tmp/foo.c", line 16: error: identifier "free" is not defined
free(temp);
^
"/tmp/foo.c", line 17: error: identifier "retun" is not defined
retun 0;
^
"/tmp/foo.c", line 17: error: expected ';'
retun 0;
^
"/tmp/foo.c", line 18: warning: function "swap" should return a value
}
^
17 errors found compiling "/tmp/foo.c".