[問題] 請問 C語言二維動態陣列從函式回傳??

作者: hunkchen2018 (女朋友消失了)   2018-05-29 14:06:06
Dear all
我用Linux下Codelite試驗一個從函式回傳二維動態陣列
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int **ptxA=(int**)malloc(sizeof(int)*10);
int **ptrB=Dynamical_memory_2_Dimensions(ptxA);
for(int loopx=0;loopx<5;loopx++)
{
for(int loopy=0;loopy<3;loopy++)
{
printf("Two Dim return function X=>%d, Y=>%d *ptr===%d\n",
loopx,loopy,*(*(ptrB+loopx)+loopy));
}
printf("\n");
}
free(ptxA);
return 0;
}
int *Dynamical_memory_2_Dimensions(int **ptxA)
{
for(int x=0; x<5;x++)
{
ptxA[x]=(int*)malloc(10*sizeof(int));
}
for(int x=0;x<5;x++)
{
for(int y=0;y<3;y++)
{
ptxA[x][y]=x*y;
}
}
return ptxA;
for(int x=0;x<5;x++)
{
free(ptxA[x]);
}
free(ptxA);
}
我目的是先傳一個二維動態陣列指標到函式
然後處理完之後再用一個新的二維動態陣列
接收回傳的二維動態陣列指標
執行完之後雖然可以執行
但是出現兩個警告訊息, 請問該如何修正??
/home/hunkchen2000/Documents/hunkchen2000/0004/main.c: In function 'main':
/home/hunkchen2000/Documents/hunkchen2000/0004/main.c:12:15: warning:
initialization from incompatible pointer type [-Wincompatible-pointer-types]
int **ptrB=Dynamical_memory_2_Dimensions(ptxA);
^
/home/hunkchen2000/Documents/hunkchen2000/0004/main.c: In function
'Dynamical_memory_2_Dimensions':
/home/hunkchen2000/Documents/hunkchen2000/0004/main.c:40:10: warning: return
from incompatible pointer type [-Wincompatible-pointer-types]
return ptxA;
^
gcc -o ./Debug/0004 @"0004.txt" -L.
make[1]: Leaving directory '/home/hunkchen2000/Documents/hunkchen2000/0004'
====0 errors, 2 warnings====
作者: joe820730 (Let it go)   2018-05-29 14:30:00
你的ptrB是兩個指標,但function的回傳值只有一個指標然後return代表function到這邊返回,所以在這之後的程式碼都不會被執行

Links booklink

Contact Us: admin [ a t ] ucptt.com