数字的地址更改为高值,然后给我一个赛段错误。我很惊讶为什么会这样,因为我从来没有做过任何指向 while-fgets 语句的指针数学。该程序应该遍历三个未排序整数的文件并将它们放在一个数组中。然后将数组进行气泡排序,然后输出到其他文本文件。这是计算机科学课。通过阅读 Franek 的 c 记忆书,我认为我为这门课做好了充分的准备,但我无法解释为什么在我创建 fgets 语句后数组的地址随机更改。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define DEBUG 1
#define TRUE 1
#define FALSE 0
#define INCREMENT_MEM 128
#define MAX_LENGTH 8
#define INIT_SIZE 8
#define NUMBER_OF_TEXT_FILES 3
int* resize(int*, int*);
void myswap(int *, int*);
int main(){
FILE *fp;
int init_size = INIT_SIZE;
int *maxsize = &init_size;
int total_newlines = -1;
int* numbers;
char str[] = "";
char file[] = "list0.txt";
int array_is_ordered = TRUE;
int i, j;
numbers = (int*) malloc (INIT_SIZE * sizeof(int) );
for(i = 0; i < NUMBER_OF_TEXT_FILES; i++){
file[4] = (char)((int)'0' + i);
fp = fopen(file , "r" );
while( fgets( str, MAX_LENGTH, fp ) != NULL){
total_newlines++;
if(total_newlines>= *maxsize){
numbers = resize(numbers, maxsize);
}
#if DEBUG == 1
printf("total_newlines=%d str=%s numbers=%d *number=%d",total_newlines, str, (int)numbers, *numbers );
#endif
*(numbers + total_newlines) = atoi(str);
}
fclose(fp);
}
int temp_int = total_newlines - INCREMENT_MEM;
numbers = resize(numbers, &temp_int);
total_newlines = temp_int;
/*
BUBBLE SORT!!!!!!!!!!!!! LULZ
*/
do{
for(i = 1; i < total_newlines; i++){
array_is_ordered = TRUE;
if( *(numbers+i-1) > *(numbers+i)){
array_is_ordered = FALSE;
myswap( &(*(numbers+i-1)) , &(*(numbers+i)) );
}
}
} while(array_is_ordered==FALSE);
/*write output code using fprintf. don't forget to close all files and free all standing memory.*/
FILE *of;
of = fopen("output.txt", "w");
printf("Opening output file for sorted digitsn");
for(i = 0 ; i < total_newlines ; i++){
fprintf(of, "%dn", *(numbers+i));
printf("%dn", *(numbers+1) );
}
fclose(of);
fclose(fp);
free(numbers);
return 0;
}
void myswap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int* resize(int* array , int* size){
int *temp;
*size = *size + INCREMENT_MEM;
temp = (int *) realloc ( array, (*size) * sizeof(int) );
if(temp!=NULL){
array = temp;
}else{
printf("Resize to %d did not work", *size);
exit(0);
}
return array;
}
更改
char str[] = "";
自
char str[MAX_LENGTH+2];
或类似。您的代码读取缓冲区 str,但 str 没有分配内存。因此,fgets 会覆盖堆栈上的一些数据,包括您的数字指针。
+2 因为我假设您的数字长达 _MAX_LENGTH_ 个,并且应该有足够的空间容纳 和零终止符。