2012-05-27 3 views
0

잠시 동안 인터넷을 살펴 봤지만 명확한 답변을 얻지 못하는 것 같습니다. C++에서 콘솔을 사용하여 간단한 sokoban 게임을 만드는 중입니다.C 배열의 함수에 2D 배열 전달하기

우선, 큰 배열 인 경우 메모리 강도 때문에 배열에 함수를 전달하는 것은 좋지 않습니다. 모두 괜찮아요. 좋은 점은 문제가되지 않아야합니다. 최대 32x41의 최대 값을 가질 수 있으며, 최대 값까지 도달하지도 못할 것입니다. 그래서 전체 배열을 전달해야합니까? 아니면 포인터를 전달해야합니까?

두 번째로, 배열 크기가 항상 같지는 않습니다. 이것이 중요한 요소인지 아닌지는 잘 모르겠습니다.

셋째, 포인터를 전달하려는 경우 어떻게 작업을 시작하기 전에 만들고/초기화해야합니까? 내 배열이 다음과 같은 방식으로 생성 된 지금

string line; 
string arr[30]; 
int i = 0; 
char mazeArr[30][40]; 
int k, count; 

if (mazeStream.is_open()) 
{ 
    while (mazeStream.good() && !mazeStream.eof()) 
    { 
     getline (mazeStream,line); 
     cout << line << endl; 
     arr[i] = line; 
     i++; 
    } 
    mazeStream.close(); 
    cout << endl; 
} 

else cout << "Unable to open file"; 

for (count = 0; count < 12; count ++) 
{ 
    string::const_iterator iterator1 = arr[count].begin(); 
    k = 0; 

    while (iterator1 != arr[count].end()) 
    { 
     mazeArr[count][k] = *iterator1; 
     iterator1++; 
     k++; 
    } 
} 

나는이 2 차원 배열 할 싶은 것이 것은 :

  • 한 번에 하나 개의 요소를 가지고 클래스의 인스턴스를 생성, 따라 어레이는 I 인스턴스의 제 2 어레이, EAC로 끝날
  • 장소 그래서 결국 인스턴스를 그 타입

소요 다른 배열로 인스턴스의 기호 h 인스턴스는 첫 번째 배열에서 가져온 심볼에 종속됩니다. 같은 '좌표'

어떤 도움도 대단히 감사하겠습니다,

감사

+1

체크 아웃 : http://stackoverflow.com/questions/6648828/passing-2d-array-as-argument – chris

+1

가능한 복제본 [어떻게 C++에서 배열을 사용합니까?] (http://stackoverflow.com)/questions/4810664/how-do-i-use-arrays-in-c) –

답변

0

을 유지하면서 나는이의 라인을 따라 뭔가를 할 것이다 :

char **createMaze(width, height) 
{ 
    // Dynamically allocate memory creating a pointer array of pointers 
    char **maze = new char*[width]; 

    // Loop to allocate memory for each pointer array 
    for(int i = 0; i < width; i++) 
     maze[i] = new char[height]; 

    return maze; 
} 

int main() 
{ 
    width = 40; 
    height = 30; 
    char **maze = createMaze(40, 30); 

    // You can now access elements from maze just like a normal 
    // 2D array maze[23][12] - You can also pass this into 
    // a function as an arugment  

    return 0; 
} 

이 코드는 안된 .. 브라우저 XD에서 작성했기 때문입니다.