Forum: PC-Programmierung call by referenz bei float zahlen anwenden


von Feldi (Gast)


Lesenswert?

Hallo,
weshalb geht folgendes nicht:
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
void main ()
5
{
6
     float x = 1.76, y = 1.88;
7
     funk( &x, &y );
8
9
         printf("%d\n",x);
10
         return 0;
11
12
13
}
14
void funk (float* a, float* b)
15
{
16
17
     *b = 2;
18
    *a =5.56 ;
19
}



Hätte die Ausgabe: 5.56 erwartet!erhalte aber
-1610612736

bei "Int" funktioniert das ganze
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
void main ()
5
{
6
     int
7
    x = 0, y = 0;
8
     funk( &x, &y );
9
10
         printf("%d\n",x);
11
         return 0;
12
13
14
}
15
void funk (int* a, int* b)
16
{
17
18
     *b = 2;
19
    *a =5 ;
20
}





Hier erhalte ich als Ausgabe den Wert 5



Gruß Feldi

von abc (Gast)


Lesenswert?

Für Float %f verwenden!

von Johann L. (gjlayde) Benutzerseite


Lesenswert?

Du tust so, also wäre dein float ein int.

Lies nochmal nach, wie format-Strings für printf et al. aufzubauen sind.

von Feldi (Gast)


Lesenswert?

Dankeschön!!
Daran habe ich nicht gedacht da ich zuvor im C++ programmiert habe und 
sich mein "cout"  selbst darum "gekümmert" hat!

Gruß Feldi

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.