动态内存访问仅在函数内部工作。
#include <stdlib.h>#include <stdio.h>void create_array (int* data, int size){ data = malloc(sizeof(*data) * size); for(int i=0; i<size; i++) { data[i] = i; } print_array(data, size);}void print_array (int* data, int size){ for(int i=0; i<size; i++) { printf("%d ", data[i]); } printf("\n");}int main (void){ int* data; const int size = 5; create_array(data, size); print_array(data, size); // crash here free(data);}
print_array
create_array
0 1 2 3 4
main
莫回无
相关分类