In C,is casting to (void*) not needed/inadvisable for memcpy() just as it is not needed for malloc()? -
i have confusions read following site memcpy()(and malloc()):
http://www.cplusplus.com/reference/cstring/memcpy/
in page,the following 2 lines stated:
destination
pointer destination array content copied, type-casted pointer of type void*.
source
pointer source of data copied, type-casted pointer of type const void*.
but after that,in code,there no casting void* in following 2 lines memcpy() used:
memcpy ( person.name, myname, strlen(myname)+1 ); memcpy ( &person_copy, &person, sizeof(person) ); please answer following 2 questions arising premise:
1) in c's case(as opposed c++) right , advisable not cast void* return type or arguments in memcpy() right , advisable not cast void* return type of malloc() in c?if so,as intuitively feel, why explicitly stated in reputed site need to cast void* (even though doesn't use in code).is site wrong it?
2) real contradiction reputed site.consider following
http://www.cplusplus.com/reference/cstdlib/malloc/
in case of malloc() ,in description, written if optional cast void* return type (exact words "..can cast desired type.."),unlike in case of memcpy() above said is cast void*.but while in memcpy() casting not done though written it cast,in case of malloc(),the casting void* done though it's written can cast void*.now see wrong in c not supposed cast malloc()'s return void*.
to put discrepancies in nutshell again lest people answering confused in wordy description:
--is advisable in c not cast void* return , arguments of memcpy()?
--is site wrong malloc() casts malloc() return void* in c code.
from iso/iec 9899:2011 of c language specification, section 6.3.2.3, page 55:
a pointer
voidmay converted or pointer object type. pointer object type may converted pointervoid, again; result shall compare equal original pointer.
so never need cast result of void* desired type, neither need opposite.
Comments
Post a Comment