請耐心等待,因爲我幾乎不知道我在說什麼。我試圖從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