[问题] 请问 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