add struct void pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> #include <stdlib.h> #include <string.h> struct Data { int num1; int num2; }; int main(void) { void *pointer = calloc (3, sizeof (struct Data)); struct Data data[3]; ((struct Data *) pointer)->num1 = 10; ((struct Data *) pointer)->num2 = 20; ((struct Data *) pointer + 1)->num1 = 30; ((struct Data *) pointer + 1)->num2 = 40; ((struct Data *) pointer + 2)->num1 = 50; ((struct Data *) pointer + 2)->num2 = 60; memcpy (data, pointer, sizeof (struct Data) * 3); printf("%d %d\n", data[1].num1, data[1].num2); printf("%d %d\n", ((struct Data*) pointer + 2)->num1, ((struct Data*) pointer + 2)->num2); free (pointer); return 0; } | cs |
0 개의 댓글:
댓글 쓰기