This works fine but I dont know how.
If it works, it does so by exercising undefined behavior. It might as easily not work, or work intermittently, or have unexpected side effects. Your compiler ought to be cluing you in to that with a warning or error such as this:
nocpy.c: In function ‘main’:
nocpy.c:12:35: warning: initialization of ‘BIGWORD *’ {aka ‘struct _BIGWORD *’} from incompatible pointer type ‘char *’ [-Wincompatible-pointer-types]
*bwBufferCast = *(BIGWORD*) { "I am trying to copy this whole text inside a buffer!" };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nocpy.c:12:35: note: (near initialization for ‘(anonymous)’)
If your compiler is not emitting a warning about that issue then you should turn up its warning level until it does, or else get a better compiler.
Note also that at your level of experience, you should treat all warnings as errors. That a program seems to do what you want when you run it is not a safe basis for concluding that it is correct for all supported inputs or that it will work as intended on other machines.
Does it copy the constant string byte after byte, does it copy it all at once
Generally speaking, there is no such thing as copying an object larger than the machine's word size "all at once". However, it may be that your hardware features an efficient, hardware-assisted memory-to-memory copy, so that at the machine level, a copy can be performed via a couple of setup instructions and one more to actually execute the copy.
or is it a just coincidence that is it there and i wouldnt work in real scenarios
It is at significant risk of misbehaving. The program accesses an object of type char[64]
as if it were an object of type struct _BIGWORD
, and this produces undefined behavior, of the whole program. Don't do this.
I know i can create char array like: char cBuffer[64] = "this is an array!"
. I want to put the const string inside a buffer at demand
That is what strcpy()
is for.
BIGWORD
. Then you dereference it, and assigning the resultingstruct
to anotherstruct
pointed bybwBufferCast
. When you assign structures, their contents are copied.char
as a structure, and no member of the structure has a type suitable for aliasing.somestruct*
with achar*
though. That's a constraint violation - the code is invalid C.