再次問候,再次感謝提供第一個問題答案的所有人。下面的代碼被更新以包含每個賦值的兩個函數。更新:C++指針代碼片段
要查看原始問題,請單擊here。
我是漂亮當然這符合作業的要求,但我再次非常感謝任何幫助。我是否適當修改了刪除語句?再次感謝。
#include<iostream>
#include<string>
int** createArray(int, int);
void deleteArray(int*[], int);
using namespace std;
int main()
{
int nRows;
int nColumns;
cout<<"Number of rows: ";
cin>>nRows;
cout<<"Number of columns: ";
cin>>nColumns;
int** ppInt = createArray(nRows, nColumns);
deleteArray(ppInt, nRows);
}
int** createArray(int nRows, int nColumns)
{
int** ppInt = new int*[nRows];
for (int nCount = 0; nCount < nRows; nCount++)
{
ppInt[nCount] = new int[nColumns];
}
return ppInt;
}
void deleteArray(int** nPointer, int nRows)
{
for (int nCount = 0; nCount < nRows; nCount++)
{
delete[] nPointer[nCount];
}
delete[] nPointer;
}
P.S.這裏是作業文檔本身,以防萬一:如果它有幫助:
(1)設計並實現一個函數爲二維整數數組分配內存:函數應該採用兩個整數作爲參數,一個用於數行數和列數。您需要在此功能中使用「新」運算符。請記住,我們需要首先創建一個指針數組。然後,對於該數組中的每個指針,我們需要創建一個整數數組。這個函數應該返回一個指向2-D整型數組的指針。 (2)設計和實現一個函數來爲這個二維數組取消分配內存:函數應該有兩個參數(一個指針指向一個二維整數數組,另一個指針指向一個二維整數數組,另一個指針指向一個二維整數數組,數組中的行數)。在函數中,你應該使用「delete」操作符爲這個2-D數組取消分配內存。您應該首先刪除每行(整數數組),然後刪除指針數組。
「創建複製函數以將數據複製到另一個2D陣列」複製構造函數有什麼問題? – 2009-02-15 05:03:52
從未提及:)我認爲你的意思是2個功能,然後沒有建議之前的類:) – 2009-02-15 05:06:03