2013-10-15 2 views
1

각각 포인터가 구조체를 가리키는 포인터의 동적 배열을 만들고 싶습니다. 프로그램에는 구조체를 추가하는 옵션이 있으며 카운터가 배열의 마지막 값에 도달하면 배열이 확장됩니다.구조체에 대한 포인터 배열에 대한 포인터를 만드는 방법은 무엇입니까?

struct student 
{ 
    string id; 
    string name; 
}; 

int N=5; 
int counter=0; 
student **big=new student *[N]; //a ptr to an array of ptr's. 

void add_student (int &counter,student **big) 
{ 
    int i; 

    if (counter==0) 
    { 
     for (i=0; i<N; i++) 
     { 
      big[i]=new student; 
     } 
    } 

    if (counter==N) 
    { 
     N+=5; 
     student **temp=new student *[N]; 
     for (i=counter-1; i<N; i++) 
     { 
      temp[i]=new student; 
     } 

     for (i=0; i<counter; i++) 
     { 
      temp[i]=big[i]; 
     } 

     delete [] big; 
     big=temp; 
    } 

    cout<<"Enter student ID: "<<endl; 
    cin>>(*big)[counter].id; 

    cout<<"Enter student name: "<<endl; 
    cin>>(*big)[counter].name; 

    counter++; 
} 

하나 이상의 학생을 추가하려고하면 프로그램을 실행할 때 충돌이 발생합니다. 감사!

+6

왜 당신은 당신의 C++ 부츠로 단계, 및 STL을 사용하지 않는 (그래서 다음과 같은 몇 가지 오류가 조심해야 할 수도 있습니다)? 이것이 특정 바퀴를 다시 구현하지 않아도되므로 * containers *가있는 이유입니다. – unwind

+0

위와 같이'student' 포인터의'std :: vector'를 사용하십시오 :'std :: vector ', 그러면'pushback'을 사용하여 새로운 포인터를 추가하십시오. –

+1

"동적 배열"을 사용하고자하는 것에 대한 C++ 질문을 계속하고 있습니다 ... 일부 학교에서는 일부 교과목 업데이트가 필요하다고 생각합니다. – crashmstr

답변

0

이 코드를 사용해보십시오. 주된 문제는 유효한 메모리가 아닌 (*big)[counter].id에 쓰는 것이 었습니다. 내 함수 아래에 학생 객체가 먼저 생성 된 다음 작성됩니다.

추신 : 나는 코드를 테스트하지 않았으므로 문제가 있는지 알려 주시기 바랍니다.

struct student { 
    string id; 
    string name; 
}; 

int N=5; 
int counter=0; 
student **big = new student *[N]; //a ptr to an array of ptr's. 

// Variable big and counter is global, no need to pass as argument. 
void add_student (student *new_student) { 
    // Resize if needed 
    if (counter==N) { 
     int i; 

     student **temp=new student *[N+5]; 

     // Copy from the old array to the new 
     for (i=0; i<N; i++) { 
      temp[i]=big[i]; 
     } 

     // Increase maximum size 
     N+=5; 

     // Delete the old 
     delete [] big; 
     big=temp; 
    } 

    // Add the new student 
    big[counter] = new_student; 

    counter++; 
} 

// Function called when we should read a student 
void read_student() { 
    student *new_student = new student; 

    cout<<"Enter student ID: "<<endl; 
    cin>>new_student->id; 

    cout<<"Enter student name: "<<endl; 
    cin>>new_student->name; 

    // Call the add function 
    add_student (new_student); 
} 
+0

학생 개체가 먼저 생성된다고 말할 때. 정확히 어디 코드에 있습니까? –

+0

그것의 여기에서 :'student * new_student = 새로운 학생;'. 새로운 학생에게 메모리를 할당 한 다음 cin <<을 사용하여 새 객체에 쓰기를합니다. – Atle

+0

작동 중! 그래서 그것에 대해 명확히하기 위해서. 구조체에 대한 포인터 배열에 대한 포인터를 만들면 객체에 쓰기를해야합니다. –

0

방금 ​​시도했습니다. 이 오류는 구조체에 대한 포인터에 대한 포인터를 올바르게 처리하지 못하기 때문에 발생합니다. 포인터를 포인터에 전달하는 것은 함수가 가리키는 포인터 주소뿐만 아니라 포인터 주소도 변경할 수 있음을 의미합니다. 그래서 함수에서 포인터에 대한 포인터를 선언해도 괜찮지 만 전역 p를 p로 선언하면 큰 의미가 없습니다. & ptr을 사용하여 동일한 효과를 얻을 수 있습니다. p부터 p까지, 즉 함수에 대한 포인터의 주소를 전달합니다. 나는 약간의 변화를 만들었지 만 효과가 있을지 확신 할 수 없다. 4/5 시간 후에 다시 시도하고 문제를 자세히 확인합니다. 당분간 다음 내용에 만족하십시오.

struct student 
    { 
     string id; 
     string name; 
    }; 

    int N=5; 
    int counter=0; 
    student *big=new student[N]; //a ptr to an array of ptr's. 

    void add_student (int &counter,student **ppBig) 
    { 
    int i; 

    if (counter==0) 
    { 
    for (i=0; i<N; i++) 
     *ppBig[i]=new student; 
    } 

if (counter==N) 
{ 
    N+=5; 
    student *temp=new student [N]; 
    for (i=counter-1; i<N; i++) 
     temp[i]=new student; 

    for (i=0; i<counter; i++) 
     temp[i]=*ppBig[i]; 

    delete[] *ppBig; 
    ppBig=temp; 
} 


cout<<"Enter student ID: "<<endl; 
cin>>(*big)[counter].id; 

cout<<"Enter student name: "<<endl; 
cin>>(*big)[counter].name; 

counter++; 

}