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

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...