3 ms·
> How do you interpret the above paragraph? It says that... int a = 5; char *b = memcpy(malloc(sizeof(a)), &a, sizeof(a)); ...should fail. The memcpy(3
by derefr 11y ago
> How do you interpret the above paragraph?
It says that...
int a = 5;
char *b = memcpy(malloc(sizeof(a)), &a, sizeof(a));
...should fail. The memcpy(3) src is &a, a pointer-to-int; the memcpy(3) dest is a pointer-to-void (i.e. an untyped pointer) from malloc(3). Thus, memcpy(3)'s return type should be a pointer-to-int.
- pklausler 11y agoIt's being assigned a pointer-to-void, yes?
- derefr 11y agoIt is, yes, according to the typespec of memcpy(3). But according to the standard, it shouldn't be; it should be returning a [whatever-type-src-was-passed-in-as]. I have doubt that there's any way in C to specify variable return type like that—but the C standard, AFAIK, doesn't require that memcpy(3)—or anything else from libc—be implementable in C. It can be, as Lisp would put it, a "special form." To get an implicit pointer-to-char-typed buffer out of memcpy(3), you should have to attach the malloc(3)-returned buffer to a pointer-to-char-typed variable (or give it an explicit cast) to give it a type, and then pass it in as dest.