Re: [問題] 函式怎麼回傳二維或三維陣列

看板C_and_CPP作者 (q82419)時間11年前 (2014/04/26 22:04), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串2/3 (看更多)
※ 引述《pudding86200 (貓頭鷹咕咕)》之銘言: : 如題 : 演算法想要用函式來取得多維矩陣的初始解 : 可是我發現會卡在一個BUG 爬文也看不懂 : 開一個矩陣ini[2][2] : 以下是我的程式碼 : 我只希望在main裡面可以呼叫test()然後把矩陣裡面的值給存取出來 : #include "stdafx.h" : #include "stdlib.h" : #include "iostream" : using namespace std; : void test(int* ini) : { : ini[0][0]=1; : ini[0][1]=2; : ini[1][0]=1; : ini[1][1]=2; : } : int _tmain(int argc, _TCHAR* argv[]) : { : int ini[2][2]={0}; : test (&ini[0][0]); : cout<<ini[0][0]<<endl<<ini[0][1]; : system("pause"); : return 0; : } : 可是執行出現了一句話 : "註標必須使用在陣列或指標型別上" : 想請問一下各位高手們我這段程式是哪邊錯了 二維以上的陣列我是習慣包成struct去傳 我習慣co C,vector或class有請版上其他神人XD #include <stdio.h> #include <stdlib.h> typedef struct ini{ int array[2][2]; } ini; void test(ini *src){ src->array[0][0] = 1; src->array[0][1] = 2; src->array[1][0] = 1; src->array[1][1] = 2; } int main(int argc, char *argv[]){ ini ini1; test(&ini1); printf("%d\n%d\n", ini1.array[0][0], ini1.array[0][1]); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.245.128 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1398521060.A.BB8.html

04/27 00:46, , 1F
感謝~~ 跟struct很不熟> <
04/27 00:46, 1F

04/27 01:32, , 2F
struct算滿基本的東西 要做linked list等等都會用到
04/27 01:32, 2F

04/27 01:32, , 3F
有時候一堆資料要綁在一起 做struct陣列就很方便
04/27 01:32, 3F

04/27 01:33, , 4F
很多library都有自定義的struct讓你用 建議熟練
04/27 01:33, 4F
文章代碼(AID): #1JMxpaku (C_and_CPP)
文章代碼(AID): #1JMxpaku (C_and_CPP)