Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

C function while(*a++ = *b++) with floats

I don't learn C and I have to tell what the function below do:

x(a,b){
   float *a,*b;
   while(*a++ = *b++);
}

I know how does this funcion with instead of floats chars work but I don't get what should this do. Copy the value or address. If value why is it in while loop?

question from:https://stackoverflow.com/questions/65540904/c-function-whilea-b-with-floats

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not even "valid" code (It would be basically valid, 20 years ago, when variables without type were assumed to be ints).

/*int*/ x(/*int*/a,/*int*/ b){
   float *a,*b;
   while(*a++ = *b++);
}

In the following, I will assume, that a and b are int* (At least in the parameters)

This is undefined behavior. float *a,*b; shadows the variables, that were given to it.

These both float pointers are uninitialized (=They can have every value possible, an access will crash the program) In the while loop, you are incrementing uninitialized pointers. This may lead to e.g. crashes.

This method is copying value for value, as long as *b is non-zero.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...