I am making a .obj loader in Objective-c and C, by following this tutorial (pure c++). But I noticed that the function fscanf() runs fine for me even if you don’t send it the memory address of the variables it going to set (sending the value only). Is there an explanation to this?
Vec2 *uv;
fscanf(file, "%f %f\n", uv.x, uv.y);
[tempUVs addObject:uv];
Notice that parameters uv.x and uv.y have no ampersand.
It’s undefined behavior when you do that. Anything could happen — it could crash, it could happen to run correctly, it could appear to run correctly while silently corrupting memory, or it could erase your hard drive. Those are all allowed behaviors according to the language standard.
If you’re compiling with a decent compiler, you can crank up your warning level (e.g. the
-Wall, and maybe also-Wextra -pedanticusing GCC or Clang), and it can warn you when you pass the wrong parameters to the*scanf/*printffamily of functions. Specifically, the-Wformatoption will warn for this sort of thing (-Wformatis included as part of-Wall).