可變長度結構體
阿新 • • 發佈:2020-10-07
// // Created by liu on 2020/10/7. // #include <stdint.h> #include <stdlib.h> #include <string.h> #include <stdio.h>struct Node { int id; int *data; }; #define case1 int main() { #ifdef case1 int DataSize = 10; struct Node node; node.data = (int *) malloc(sizeof(int) * DataSize); node.data[0] = 1; node.data[1] = 5; for (int i = 0; i < 10; i++) { printf("node.data[%d]:%d\n", i, node.data[i]); } #endif #ifdef case2 int DataSize = 10; int data[DataSize]; struct Node node; node.data = data; node.data[0] = 10; node.data[1] = 50; for (int i = 0; i < DataSize; i++) { printf("node.data[%d]:%d\n",i ,node.data[i]); } #endif #ifdef case3 int DataSize = 10; struct Node * node = (struct Node *)malloc(sizeof(struct Node) + DataSize); node->data = node + sizeof(struct Node); node->data[0] = 100; node->data[1] = 500; for (int i = 0; i < DataSize; i++) { printf("node.data[%d]:%d\n",i ,node->data[i]); } #endif return 0; }