“ int * nums = {5,2,1,4}”导致分段错误
int *nums = {5, 2, 1, 4}; printf("%d\n", nums[0]); 导致段错误,而 int nums[] = {5, 2, 1, 4}; printf("%d\n", nums[0]); 没有。现在: int *nums = {5, 2, 1, 4}; printf("%d\n", nums); 打印5。 基于此,我猜想数组初始化符号{}将数据盲目地加载到左侧的任何变量中。当它为int []时,将根据需要填充数组。当它为int *时,指针将被5填充,指针存储之后的内存位置将被2、1和4填充。因此nums [0]尝试取消引用5,从而导致段错误。 如果我错了,请纠正我。如果我是正确的,请详细说明,因为我不理解为什么数组初始化程序会以它们的方式工作。