C++ 如何创建指向结构的指针数组的指针?
我想创建一个指针的动态数组,每个指针都指向一个结构。在程序中,有一个添加结构的选项,如果计数器达到数组值的最后一个值,数组将展开C++ 如何创建指向结构的指针数组的指针?,c++,arrays,pointers,struct,delete-operator,C++,Arrays,Pointers,Struct,Delete Operator,我想创建一个指针的动态数组,每个指针都指向一个结构。在程序中,有一个添加结构的选项,如果计数器达到数组值的最后一个值,数组将展开 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 **
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++;
}
struct学生
{
字符串id;
字符串名;
};
int N=5;
int计数器=0;
学生**大=新学生*[N]//一个ptr到一个ptr阵列。
无效添加学生(整数和计数器,学生**大)
{
int i;
如果(计数器==0)
{
对于(i=0;i请尝试此代码。主要问题是写入(*big)[counter].id
,但这不是有效内存。在我下面的函数中,首先创建学生对象,然后写入
PS:我还没有测试代码,如果有问题请告诉我
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);
}
struct学生{
字符串id;
字符串名;
};
int N=5;
int计数器=0;
学生**大=新学生*[N];//一个ptr到一个ptr数组。
//变量大,计数器是全局的,不需要作为参数传递。
无效添加学生(学生*新学生){
//根据需要调整大小
如果(计数器==N){
int i;
学生**临时=新学生*[N+5];
//从旧阵列复制到新阵列
对于(i=0;i我刚刚尝试过。错误是因为您没有正确处理指向结构的指针。传递指向某个对象的指针意味着函数可以更改指针地址,而不仅仅是它指向的对象。因此,在函数中声明指向指针的指针是可以的,但将全局p声明为p到s没有多大意义。哟您可以使用&ptr实现相同的效果。对于p到p到s,即将指针的地址传递到函数。我做了一些更改,但不确定它是否有效。我将在4/5小时后重试,并将详细检查问题。暂时请满足以下要求。(可能是下面的一些错误,请小心)
struct学生
{
字符串id;
字符串名;
};
int N=5;
int计数器=0;
student*big=new student[N];//一个ptr到一个ptr数组。
无效添加学生(整数和计数器,学生**ppBig)
{
int i;
如果(计数器==0)
{
对于(i=0;为什么不直接进入C++引导程序,使用STL?这就是为什么它有容器,以避免您不得不重新实现这个特定的轮子。如上,使用<代码> STD::向量<代码> <代码>学生< /Cords>指针,如:<代码> STD::vector < /代码>,然后使用<代码> PurbSub/Cuff>添加新指针。我继续看到C++。关于希望使用“动态数组”的说明…我猜一些学校的课程作业需要更新。当你说首先创建了一个学生对象时。它到底在代码中的什么地方?它在这里:student*new\u student=new student;
。我为一个新学生分配内存,然后使用cinIt!所以要弄清楚。如果我创建指向str的指针数组的指针UCT,我必须创建要在其上写入的对象。是的。student**big=new student*[N];
所做的只是分配内存以容纳N个指针。它们最初并不指向任何有效的内存地址。
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++;