Swap function??

Can some1 also help with a swap function:

here is my code it doesn't work :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

void swap (int & a, int & b)

{

int x;
x = a;
a = b;
b = x;

}


void main()

{

int m, n;

m = 6; n = 7;

printf("m = %d and n = %d \n", m, n);

swap (m, n);

printf("m = %d and n = %d \n", m, n);

}
Why doesn't it work ( post errors )
Are you using C or C++ ?
Works perfectly if your main returns int (and if you compile it with g++).
Error E2293 exercise3.c 4: ) expected
Error E2206 exercise3.c 24: Illegal character 'ô' (0x93)
Error E2206 exercise3.c 24: Illegal character '\' (0x5c)
Error E2206 exercise3.c 24: Illegal character 'ö' (0x94)
Error E2206 exercise3.c 28: Illegal character 'ô' (0x93)
Error E2206 exercise3.c 28: Illegal character '\' (0x5c)
Error E2206 exercise3.c 28: Illegal character 'ö' (0x94)
*** 7 errors in Compile ***
Are you using a C compiler?
And check your file encoding
you are taking an integer value from a pointer without a cast, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>

void swap (int *a, int *b)

{

int x;
x = *a;
*a = *b;
*b = x;

}


void main()

{

int m, n;

m = 6; n = 7;

printf("m = %d and n = %d \n", m, n);

swap (&m, &n);

printf("m = %d and n = %d \n", m, n);
getchar();
}
Last edited on
The code caffrea4 posted was using references, not pointers. And it was using them correctly.
Yes but if it was using a C compiler, there's no chance that references will work
Topic archived. No new replies allowed.