作者hunkchen2018 (女朋友消失了)
看板C_and_CPP
标题[问题] 请问 C语言二维动态阵列从函式回传??
时间Tue May 29 14:06:06 2018
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====
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.230.250.38
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1527573968.A.7EA.html
1F:→ joe820730: 你的ptrB是两个指标,但function的回传值只有一个指标 05/29 14:30
2F:→ joe820730: 然後return代表function到这边返回,所以在这之後的程 05/29 14:31
3F:→ joe820730: 式码都不会被执行 05/29 14:31