我正在尝试运行此代码;编译代码很好,有一些警告,但当我尝试运行它只是崩溃。这是我试图运行的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct maze_data{
int* maze_array;
int* start_location;
int* end_location;
int* rows;
int* columns;
};
struct maze_data get_data(void);
void main(){
struct maze_data data_object;
data_object = get_data();
}
struct maze_data get_data(void) {
char c;
int i = 0;
int j = 0;
int k = 0;
int numberlength = 0;
char number[2];
int size_of_file;
int number_of_numbers;
char characters_in_file[100000];
int numbers_in_file[100000];
int space_locations[100000];
char fname[100] = "";
int rows;
int columns;
int mazenumbers[1000][1000];
struct maze_data data_object;
int start[2];
int end[2];
printf("enter file name: ");
gets(fname);
FILE *fpoint;
fopen_s(&fpoint, fname, "r+");
if (fpoint == NULL) {
printf("No Such File !! ");
return;
}
while (1) {
c = fgetc(fpoint);
if (feof(fpoint)) {
printf("\n");
size_of_file = i - 1;
break;
}
characters_in_file[i] = c;
i++;
}
for (i = size_of_file; i >= 0; i--) {
characters_in_file[i + 1] = characters_in_file[i];
}
characters_in_file[0] = ' ';
for (i = 0; i < (size_of_file + 1); i++) {
if (characters_in_file[i] == '\n' || characters_in_file[i] == ' ') {
space_locations[j] = i;
j++;
}
}
number_of_numbers = j - 1;
for (i = 0; i < number_of_numbers; i++) {
numberlength = (space_locations[i + 1] - 1) - (space_locations[i]);
number[0] = characters_in_file[space_locations[i]+1];
if (numberlength == 2) {
number[1] = characters_in_file[space_locations[i + 1] - 1];
}
else {
number[1] = ' ';
}
if (numberlength > 0) {
j = atoi(number);
numbers_in_file[i] = j;
}
}
for (i = 0; i < number_of_numbers; i++) {
if (numbers_in_file[i] < 0) {
for (j = 0; j < number_of_numbers; j++) {
numbers_in_file[j + i] = numbers_in_file[j + i + 1];
}
number_of_numbers--;
}
}
rows = numbers_in_file[0];
columns = numbers_in_file[1];
start[0] = numbers_in_file[2];
start[1] = numbers_in_file[3];
end[0] = numbers_in_file[4];
end[1] = numbers_in_file[5];
k = 7;
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
mazenumbers[i][j] = numbers_in_file[k];
k++;
}
}
data_object.maze_array = mazenumbers;
data_object.start_location = start;
data_object.end_location = end;
data_object.rows = rows;
data_object.columns = columns;
return data_object;
}
我收到了这些错误:
(38):警告C4013:&#39;得到&#39;不确定的;假设extern返回int
(44):警告C4033:&#39; get_data&#39;必须返回一个值
(123):警告C4047:&#39; =&#39;:&#39; int *&#39;与
的间接水平不同&#39; INT(*)[1000]&#39;
(126):警告C4047:&#39; =&#39;:&#39; int *&#39;间接水平与&#39; int&#39;
不同(127):警告C4047:&#39; =&#39;:&#39; int *&#39;间接水平与&#39; int&#39;
不同(131):警告C4715:&#39; get_data&#39;:并非所有控制路径都返回值
有人可以解释一下是什么问题。谢谢。
答案 0 :(得分:0)
get_data()
被定义为返回struct maze_data
,所以它必须始终这样做,即使对于错误条件:
if (fpoint == NULL) {
printf("No Such File !! ");
return;
}
由于这是检查可能是致命错误的原因,因此修复它的一种简单方法是将return
替换为exit(1);
您可以将结构定义为具有整数的指针:
struct maze_data {
int* maze_array;
int* start_location;
int* end_location;
int* rows;
int* columns;
}
然后尝试将一个数组数组分配给一个指针:
int mazenumbers[1000][1000];
data_object.maze_array = mazenumbers;
其他指针的整数值:
int rows;
int columns;
data_object.rows = rows;
data_object.columns = columns;
对于行和列,看起来您应该只将结构定义更改为
int rows;
int columns;
对于maze_array
,它完全取决于您要返回的内容...是整个mazenumbers
数组数组,还是只是某一行?无论哪种方式,如果要返回指向内存的指针,就无法静态为mazenumbers
分配内存,您需要使用malloc()
为其分配堆内存。