代码1:为什么可以通过for循环分配i的内存???
#include<stdio.h> #include<malloc.h> #include<stdlib.h> int main() { int j; for (j=0; j<5; j++) { int * i = (int *)malloc(sizeof(int)); printf("内存地址:%d\n", i); } return 0; } /*输出结果: 内存地址:5312288 内存地址:5312320 内存地址:5312336 内存地址:5312352 内存地址:5312368 */
代码2:为什么这样就提示错误??
#include<stdio.h> #include<malloc.h> #include<stdlib.h> int main() { int * i = (int *)malloc(sizeof(int)); printf("内存地址:%d\n", i); int * i = (int *)malloc(sizeof(int)); printf("内存地址:%d\n", i); int * i = (int *)malloc(sizeof(int)); printf("内存地址:%d\n", i); return 0; } /*提示错误: a.c: In function 'main': a.c:10:8: error: redefinition of 'i' a.c:8:8: note: previous definition of 'i' was here a.c:12:8: error: redefinition of 'i' a.c:10:8: note: previous definition of 'i' was here */
问题:问什么代码1可以重复分配i的内存而代码2不可以,分配出来5块内存都是以i为命名的吗?不会命名冲突吗?
初学程序小白,望大神搭救!!
Shanhui
慕粉3754631
农民爸爸
相关分类