어떤 이유로 while 루프를 종료 한 후에 배열의 값이 변경되지만 그 이유는 아닙니다. 루프 안에서는 값이 정확하며 배열 (a) 내부에 값을 저장합니다. 이것은 c 다!루프를 종료 한 후 배열의 값이 변경되는 이유는 무엇입니까?
int * readFile()
{
char file_name[50];
printf("Enter the name of the file to open:\n");
scanf("%s",file_name);
FILE *fp;
fp = fopen(file_name,"r");
if(fp == NULL)
{
printf("Sorry but the File you entered cannot be opened\n");
int *b;
b[0] = -1;
return b;
}
int *a;
int j=0;
long int value=0;
while (fscanf(fp,"%d",&value)!=EOF) {
if((a =malloc(sizeof(long int))) == NULL)
printf("not enough memory\n");
a[j]=value;
j++;
}
printf("%d %d %d\n",a[0],a[1],a[2]);
int i=0;
for(i=0; i<j;i++)
{
printf("array[%d] = %d\n",i,a[i]);
}
fclose(fp);
return a;
}
모든 도움을 주시면 감사하겠습니다.
언어는 유익한 정보입니다. –
이것은 c입니다! 죄송합니다 깜박 했어요 – Jeremy
'if ((a = malloc (size int)) == NULL)'은 정수 크기의 새로운 메모리 블록을 할당합니다. 그런 다음'j> 0 '에 존재하지 않는'j' 번째 슬롯에 씁니다. –