我以前問過這個問題:Experimenting with pointers of arrays in C關於傳遞指向函數的指針和修改函數中的現有數組,以便將數組「返回」到main()
。我留下了一個問題,這兩個代碼塊有什麼區別?用C返回數組,不同的方法
// #includes
int ***myfunc(int***);
int main()
{
int a***;
//allocate memory for array 'a'
a = myfunc(a);
// display all contents of array 'a'
return 0;
}
OR
// #includes
void myfunc(int***);
int main()
{
int a***;
//allocate memory for array 'a'
myfunc(a);
// display all contents of array 'a'
return 0;
}
兩種功能將修改陣列和當displaying all contents of a
,其內容將是相同的。
我希望這不會造成混淆,我想我可以在需要的時候闡述更多。
一個顯著EDITTTTTTTTT
下面是兩個工作示例(據我所知):
#include "stdlib.h"
#include "stdio.h"
void myfunc(int**);
int main() {
int **a,i, j;
a = (int**) calloc(10,sizeof(int*));
for (i=0; i<10; i++)
a[i] = (int*) calloc(10, sizeof(int));
myfunc(a);
for (i=0; i<10; i++) {
for (j=0; j<10; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
void myfunc(int** a) {
int i, j;
for (i=0; i<10; i++)
for (j=0; j<10; j++)
a[i][j] = i*j;
}
AND ...
#include "stdlib.h"
#include "stdio.h"
// returning arrays through functions
int **myfunc(int **b);
int main() {
int **a,i,j;
a = (int**) calloc(10,sizeof(int*));
for (i=0; i<10; i++)
a[i] = (int*) calloc(10, sizeof(int));
a = myfunc(a);
for (i=0; i<10; i++) {
for (j=0; j<10; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
int **myfunc(int **b) {
int i, j;
for (i=0; i<10; i++)
for (j=0; j<10; j++)
b[i][j] = i*j;
return b;
}
在那裏做了那麼多「***」? – 2011-04-24 15:40:25
我認爲不同的是,在第一個版本中,至少可以保存一個'*'。如果您認爲需要指向指針的指針,則可能需要重新考慮問題。 – 2011-04-24 15:45:44
3d數組。我們可以將它用於所有密集型目的 – Amit 2011-04-24 15:45:54