[問題] 動態的字元陣列? (或動靜態陣列混寫)

看板C_and_CPP (C/C++)作者 (兔子)時間15年前 (2011/01/13 17:00), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) dev-c++ 問題(Question): 多維陣列中 如果某一維是靜態其他是動態 要怎麼寫? 程式碼(Code):(請善用置底文網頁, 記得排版) 字元陣列是這樣寫 #include<iostream> using namespace std; int main(){ char s[][7]={{'d','o','g','\0'}, {'r','a','b','b','i','t','\0'}, {'c','a','t','\0'}}; cout<<s[1]; system("pause"); return 0; } 動態陣列是這樣寫 #include<iostream> using namespace std; int main(){ char**s; s=new char*[3]; s[0]=new char[4]; s[0][0]='d'; s[0][1]='o'; s[0][2]='g'; s[0][3]='\0'; s[1]=new char[7]; s[1][0]='r'; s[1][1]='a'; s[1][2]='b'; s[1][3]='b'; s[1][4]='i'; s[1][5]='t'; s[1][6]='\0'; s[2]=new char[4]; s[2][0]='c'; s[2][1]='a'; s[2][2]='t'; s[2][3]='\0'; cout<<s[1]; system("pause"); return 0; } 前一維靜態後一維動態 #include<iostream> using namespace std; int main(){ char *s[3]; s[0]=new char[4]; s[0][0]='d'; s[0][1]='o'; s[0][2]='g'; s[0][3]='\0'; s[1]=new char[7]; s[1][0]='r'; s[1][1]='a'; s[1][2]='b'; s[1][3]='b'; s[1][4]='i'; s[1][5]='t'; s[1][6]='\0'; s[2]=new char[4]; s[2][0]='c'; s[2][1]='a'; s[2][2]='t'; s[2][3]='\0'; cout<<s[1]; system("pause"); return 0; } 但是我是要前一維動態後一維靜態啊 (不知道動物數量 但確定他們的名字都在7字以內) 這樣是不是沒辦法做到呢? -- ◤ __ \__◢◤◢◤ ψhirabbitt ◤ ◢███◣ ◢███◣ 我不是m █████ █████ 我不是s ██ ███ 是溫柔 ██╱╲ ◥█████◣● 是傲嬌╱╱ ◥████ ˇ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.13.127.91

01/13 17:04, , 1F
都c++了 不考慮string class嗎^^?
01/13 17:04, 1F

01/13 17:33, , 2F
我也推 string, 如果真的要這麼做的話..
01/13 17:33, 2F

01/13 17:34, , 3F
01/13 17:34, 3F

01/13 17:36, , 4F
(少了 delete 自己補上去)
01/13 17:36, 4F

01/14 10:45, , 5F
喔喔 感謝^^/
01/14 10:45, 5F
文章代碼(AID): #1DBhz3uo (C_and_CPP)
文章代碼(AID): #1DBhz3uo (C_and_CPP)