Home > Back-end >  Cancel the NULL pointer reference of XXX
Cancel the NULL pointer reference of XXX

Time:12-04

#include
Void splitfloat (float x, int * intpart, float * fracpart) {
Float number;
int count=0;
Number=x;
While (number & gt;=1) {
count++;
Number -=1;
}
Intpart=& amp; The count.
Fracpart=& amp; Number;
* intpart=count;
* fracpart=number;
}
Int main (void)
{
Float x * fracpart1;
Int * intpart1;
Scanf_s (" % f ", & amp; X);
Fracpart1=0;
Intpart1=0;
Splitfloat (x, intpart1, fracpart1);
Printf (" intpart is % d, fracpart is % f ", * intpart1, * fracpart1);
return 0;
}

Bosses can see me?

CodePudding user response:

Temporary variable address is assigned to a pointer

CodePudding user response:

Inputpart1 point to a local variable, because local variables starts from the definition of life cycle to the end of the function returns, so the output is invalid of the local variable is undefined behavior

CodePudding user response:

Upstairs, code changed slightly,
 # include 

Void splitfloat (float x, int * intpart, float * fracpart) {
int count=0;
While (x & gt;=1) {
count++;
X=1;
}
* intpart=count;
* fracpart=x;
}
Int main (void)
{
Float x, fracpart1;
Int intpart1;
Printf (" Enter a number: ");
Scanf_s (" % f ", & amp; X);
Splitfloat (x, & amp; Intpart1, & amp; Fracpart1);
Printf (" intpart is % d, fracpart is % f \ n ", intpart1, fracpart1);
return 0;
}

  • Related