I am trying to make a swap function on my own with float pointers and it just does not work. For some reason I think I don't pass the float pointers to the function in the correct way.
#include <stdio.h>
#include <stdlib.h>
void swap(float *a, float *b);
int main() {
float num1 = 0.0;
float num2 = 0.0;
float *px = NULL;
float *py = NULL;
printf("Please enter a decimal number: ");
scanf("%f", &num1);
getchar();
printf("Please enter another decimal number: ");
scanf("%f", &num2);
px = &num1;
py = &num2;
printf("The numbers before swapping - \nNum1 = %f\nNum2 = %f\n", num1, num2);
swap(&px, &py);
printf("\nThe numbers after swapping - \nNum1 = %f\nNum2 = %f\n\n", num1, num2);
system("PAUSE");
return 0;
}
void swap(float *a, float *b) {
float temp = *a;
*a = *b;
*b = temp;
}
pxandpyin this code, passing&num1and&num2to your swap instead. Or lose the&in front ofpxandpyin your swap call. If you didn't get at least a warning aboutfloat**not being compatible withfloat*in this, I'm shocked.gcc -c -Wall -Wextra -Wconversion -std=gnu99 file.c -o file.othe compiler output: 1) line:4: passing argument 1 of 'swap' from incompatible pointer type 2) line 25: passing argument 2 of 'swap' from incompatible pointer type 3) line4 expected 'float*' but argument is of type 'float**'. Suggest fixing those problems before trying anything else. Note: do not ignore warnings.