-12

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?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
GoldenRC
  • 91
  • 2
  • 8

1 Answers1

3

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.

JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
  • Thank you, that's what I tought. The code wasn't valid. Now I understand it. – GoldenRC Jan 02 '21 at 15:57
  • 1
    Consider accepting this answer, it I could help you – JCWasmx86 Jan 02 '21 at 17:16
  • 2
    @GoldenRC Or, if you maybe meant `x(a,b) float *a,*b; {...}` then that would be a [K&R style](https://stackoverflow.com/questions/1630631/alternative-kr-c-syntax-for-function-declaration-versus-prototypes) syntax that has been deprecated for more than 30 years now. – dxiv Jan 02 '21 at 18:44