-2

I'm trying to swap two floats using pointers ,

My code :

#include <stdio.h>

void swap(float* p, float* q);


int main() {
    double num1,num2;
    printf("Please write 2 numbers ");
    scanf("%lf %lf",&num1 , &num2);
    swap(&num1,&num2);
    printf("\nnum1 is  : %.2lf \n",num1);
    printf("num2 is  : %.2lf \n",num2);

    return 0;
}

void swap(float* p, float* q){
    float a;
    a=*p;
    *p=*q;
    *q=a;

}

My issues :

  1. The code doesn't swap
  2. The pointers in the swap function show value 0 for the pointer of the address I'm sending .

For example - input 99 22 -> gives me 0 :

Using Debugger I get

I don't get why I'm getting that , if according to this comment I have an address that I sent (let's assume I sent 00001 ) then a pointer to that address would be my value (99 in this example ) but I get 0 ...

I'd appreciate any help !

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Luuaai
  • 13
  • 3
  • 7
    You are mixing double and float here. Even worse, pointers to them. This is definitely not ok. Please try again with either all doubles or all floats. – PhilMasteG Jun 01 '22 at 21:02
  • Every self-respecting C or C++ compiler will refuse to compile this. Which compiler are you using? – Sam Varshavchik Jun 01 '22 at 21:04
  • You are passing pointers to `double` to a swap function that expects its arguments to be pointers to `float`. If your compiler is not complaining about that then find the dial to turn up the warnings, or get a better compiler. With that mismatch, the execution of `swap()` has undefined behavior, which could certainly manifest with the symptoms you describe. – John Bollinger Jun 01 '22 at 21:05
  • @SamVarshavchik: No, with default settings, they will just issue warnings. – Eric Postpischil Jun 01 '22 at 21:05
  • @eric - the result from gcc with default settings: `t.C:10:10: error: cannot convert ‘double*’ to ‘float*’` -- a little bit more than just a warning. No soup for you. – Sam Varshavchik Jun 01 '22 at 21:08
  • on my side `gcc -Wall -g` throw warnings but compile without error... –  Jun 01 '22 at 21:18
  • only warnings with `-Wall -Wextra` using [gcc 12.1](https://godbolt.org/z/KGbG3GY7M) and [clang 14.0.0](https://godbolt.org/z/eTjrE5sh6) – yano Jun 01 '22 at 21:20
  • @SamVarshavchik: [GCC 12.1 with no switches produces only warnings.](https://godbolt.org/z/YEdoor8Gj) – Eric Postpischil Jun 01 '22 at 21:33
  • @SamVarshavchik: As C++, GCC issues an error rather than a warning. – Eric Postpischil Jun 01 '22 at 21:42
  • Most important : never ignore compiler warnings : try to understand what is wrong. Sometimes warning may not be harmful but never rely on luck. – Ptit Xav Jun 01 '22 at 21:51
  • `gcc (GCC) 12.1.1 20220507 (Red Hat 12.1.1-1)` results in an error, @EricPostpischil – Sam Varshavchik Jun 01 '22 at 21:57
  • @SamVarshavchik: Read all the comments. – Eric Postpischil Jun 02 '22 at 01:47
  • I appreciate the help , I didn't catch that and thought it was something more complicated with pointers , Thank you guys. – Luuaai Jun 02 '22 at 06:10

1 Answers1

1

As pointed out by comments, the problem is that your swap function takes float pointers while you are using double variables.

Since float type is used to describe floating point value over 4 bytes (32 bits) and the double type describes floating point value over 8 bytes (64 bits), when you pass double pointers to a function that treat them as float type, the values are wrongly assigned (over 4 bytes instead of 8 bytes) from one memory area to another.

The half of bits of a double does not make a valid corresponding float.