请耐心等待,因为我几乎不知道我在说什么。我试图从PGM文件读取数据(不包括标题)。我有一个函数(read_data),它接受一个双指针(char **数据)作为参数。指向一维数组的双指针
在函数中,我可以将数据放入一个数组(arr),但我的问题是将该指针(**数据)分配给该数组。这是我的代码到目前为止:
read_data(char* file, char** data) {
FILE *fp = fopen(file, "rb");
char type[3], dump;
int i =0, data_length;
int w, h; // width & height
fgets(type, sizeof(type), fp);
fscanf(fp, "%d", &w);
fscanf(fp, "%d", &h);
if (strcmp(type, "P5 ")) {
data_length = w * h;
}
else {
data_length = (w * h) * 3;
} // this is to work out the size of the array (how many bits of data are in the file)
char arr[data_length];
while ((dump = fgetc(fp)) != '\n'); // just to skip the header
for (i; i < data_length; i++) {
arr[i] = fgetc(fp);
}
data = &arr; // <--- The problem
fclose(fp);
return data_length;
}
main() {
int data_length;
char **data = malloc(sizeof(int*)*data_length);
read_data("file.pgm",data);
}
希望这是可读的。谢谢。
什么是'image_data'?它在哪里宣布?你为什么认为你可以分配给一个数组?数组是不可修改的l值,您不能分配给数组。 – 2014-09-03 04:32:01