+ 2

Parameter transfer to functions via pointer in C

Hi, i got a code from a book of my university and it does not work. Error message: warning: 'swap' defined but not used [-Wunused-function] void swap(int *a, int *b) ^~~~ ..\Playground\~1\AppData\Local\Temp\cc2dRKGB.o:source.c:(.text+0x46): undefined reference to `swap' collect2.exe: error: ld returned 1 exit status Even if i try other online C Compilers the message is the same. (I simply want to change two Variables) Code: #include <stdio.h> void swap(int *a, int *b); int main() { int zahl1, zahl2; zahl1 = 10; zahl2 = 20; printf("Startvalue: Zahl1: %i, Zahl2: %i", zahl1, zahl2); swap(&zahl1, &zahl2); printf("Finalvalue: Zahl1: %i, Zahl2: %i", zahl1, zahl2); return 0; void swap(int *a, int *b) { int vhilf; vhilf = *a; *a = *b; *b = vhilf; } }

13th Apr 2019, 1:00 PM
Maximilian Schuh
2 ответов
+ 10
Your swap() function needs to be defined outside of main() and at least its prototype needs to be provided before the main() function.
13th Apr 2019, 1:16 PM
Sonic
Sonic - avatar
+ 2
as Sonic have said, just erase the last semicolon and put one after the return 0
14th Apr 2019, 6:56 AM
✳AsterisK✳
✳AsterisK✳ - avatar