我想用「數字」結構元素製作一個具有偶數矩陣的矩陣。當我嘗試打印「theNumber」時,程序不會退出printf函數。它始終爲我提供零printf.Maybe我做了錯誤的分配?有什麼想法嗎?未能從C中的結構打印字段
struct number {
int theNumber;
};
struct table {
struct number ** mem;
};
unsigned i;
struct table *createInstance(unsigned min) {
//flag1
struct table * someInstance = (struct table *) malloc(sizeof (struct table));
//flag2
if (someInstannce == NULL) {
return 0;
}
//flag3
someInstannce->mem = (struct number **) malloc((min + 1) * sizeof (struct number **));
//flag4
for (i = 0; i <= min; i++) {
someInstannce->mem[i] = (struct table *) calloc(5, sizeof (struct number *) + 1);
}
//flag5
for (i = 0; i <= min; i++) {
for (j = 0; j = 5; j++) {
printf("%d", someInstannce->mem[i][j].theNumber);
}
}
return someInstance;
}
因爲這是嚴格的C,所以你不能強制轉換malloc的返回值。 – sjsam
您正在使用'someInstance'和'someInstannce'。這些拼寫錯誤,還是有另一個變量的名稱?這段代碼「類似」你的代碼,但不是*實際上*你的代碼? –
@sjsam:將'void *'強制轉換爲其他指針是標準allowe3d。儘管如此,這是令人沮喪的。 – Olaf