i want put literal array dynamic memory.
double *rgb = (double*)malloc(3 * sizeof(double)); memcpy(rgb, (double []){1,2,3}, sizeof(rgb)); but error: many arguments provided function-like macro invocation.
but if this:
double *rgb = (double*)malloc(3 * sizeof(double)); memcpy(rgb, (double []){1}, sizeof(rgb)); the compiler yields no errors? why?
is objective-c bugged ... or missing something?
too many arguments provided function-like macro invocation.
memcpy() apparently implemented compiler/stdlib preprocessor function macro (i.e. #define somewhere in lib's header files). line
memcpy(rgb, (double []){1,2,3}, sizeof(rgb)); is reading commas inside , considering them argument separators (remember, preprocessor knows nothing c code except token is). preprocessor, have 5 arguments rgb, (double []){1, 2, 3}, , sizeof(rgb). expects 3 arguments.
with regard how initialise dynamic memory, works gcc in c99 standard mode:
double *rgb = (double*)malloc(3 * sizeof(double)); memcpy(rgb, (double []){1,2,3}, 3 * sizeof(double)); ... apparently gcc's stdlib not use macro memcpy.
also note sizeof(rgb) = 4 or 8, because rgb pointer: sizeof pointer size of memory address, not of length of array. thus, used 3 * sizeof(double) in memcpy call.
in more complex program, have pass length of array around along pointer array, in order information available code manipulates array.
for particular compiler, may solution (although if isn't optimised away, it's less efficient—presumably wouldn't hard-coding initialisation data that's large enough matter?):
double rgb_initial[] = {1, 2, 3}; double * rgb = (double *) malloc(3 * sizeof(double)); memcpy(rgb, rgb_initial, 3 * sizeof(double));
Comments
Post a Comment