undefined reference to `swap'|

Hi all! it's my first time posting on this forum altho, I used it for a while now.
I tried to find the problem in my code when writing a simple swap function with pointers in C. but no matter what I do, im still getting same error.

undefined reference to `swap'|

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
#include <stdio.h>

int swap(int *a, int *b);
int main()
{
	int x, y;

	printf("Enter 2 integers:\n");
	scanf("%d %d", &x, &y);
	printf("%d %d\n", x, y);
	swap(&x,&y);
	printf("%d %d", x, y);
	return 0;


int swap(int *a, int *b)
{
 int tmp;

tmp = *a;
*a = *b;
*b = tmp;
return 0;
}


}
Last edited on
Define swap() outside of main(). After line 27
Tried to. didnt help. not at line 27 nor before main. same Error.
Yeah, the problem is that you can't define functions inside other functions.
Thank you coder77 and peter87. I defined it outside the main function in the proper way. it's working now.
Last edited on
what compiler do you use? There should be a comiler error when swap() is defined inside main().

If you define swap() outside main() the linker should be satisfied except there's a typo in the definition.

By the way: line 20 is wrong it should be tmp = *a;
Nah, i just defined it wrong. after few tries and some google. i defined it in the proper way. and about line 20: yeah i figured it out after i compiled correctly.
thank you very much for your help.
Topic archived. No new replies allowed.