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

看板C_and_CPP (C/C++)作者 (女朋友消失了)時間7年前 (2018/05/29 14:06), 編輯推噓0(003)
留言3則, 1人參與, 7年前最新討論串1/1
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://www.ptt.cc/bbs/C_and_CPP/M.1527573968.A.7EA.html

05/29 14:30, 7年前 , 1F
你的ptrB是兩個指標,但function的回傳值只有一個指標
05/29 14:30, 1F

05/29 14:31, 7年前 , 2F
然後return代表function到這邊返回,所以在這之後的程
05/29 14:31, 2F

05/29 14:31, 7年前 , 3F
式碼都不會被執行
05/29 14:31, 3F
文章代碼(AID): #1R3EtGVg (C_and_CPP)
文章代碼(AID): #1R3EtGVg (C_and_CPP)