我正在嘗試使二維數組的功能,但不知何故,它不工作。代碼在這裏:C中的二維數組malloc - >錯誤?
#include <stdio.h>
#include <stdlib.h>
int **multiTable (unsigned int xs, unsigned in ys)
{
unsigned int i, j;
int **table = int(**)malloc(xs * ys * sizeof(int*));
for(i = 0; i < ys; i++)
{
for(j = 0; j < xs; j++)
{
table[i][j] = j * i;
}
}
free(**table);
return table;
}
所以首先,我還應該在malloc裏面添加行(xs)嗎?還是應該工作,如果我只與列一起工作? - >是這樣的:
int **table = int(**)malloc(ys * sizeof(int*));
你不能在「一杆」中做到這一點。首先將內存分配給'table',然後將'table [i]'(s)分配給內存。 – 2014-12-02 12:31:04
什麼是'返回'的東西,只是'免費'的一點? – 2014-12-02 12:34:06