gpt4 book ai didi

c - 'int *' differs in levels of indirection from ' int'

转载 作者:行者123 更新时间:2023-11-30 21:06:13 25 4
gpt4 key购买 nike

我正在尝试运行此代码;编译代码很好,有一些警告,但是当我尝试运行它时它就会崩溃。这是我尝试运行的代码:

#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:“获取”未定义;假设 extern 返回 int

(44):警告 C4033:“get_data”必须返回一个值

(123): 警告 C4047: '=': 'int *' 与

的间接级别不同

'int(*)[1000]'

(126): 警告 C4047: '=': 'int *' 与 'int' 的间接级别不同

(127): 警告 C4047: '=': 'int *' 与 'int' 的间接级别不同

(131):警告 C4715:“get_data”:并非所有控制路径都返回值

有人可以解释一下出了什么问题吗?谢谢。

最佳答案

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() 为 mazenumbers 分配堆内存它。

关于c - 'int *' differs in levels of indirection from ' int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50111077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com