注册 登录
编程论坛 C++教室

关于指针数组作为参数的问题...

无名可用 发布于 2010-08-20 23:35, 1068 次点击
我刚接触c++,知道结构体属于值类型,所以一个结构体类型变量作为参数是值传递;而数组作为参数是地址传递,因此当一个结构体数组作为参数时传递的是该数组的首地址。。
但我对指针数组作为参数传递还是不太清楚。.
为了简化问题,代码有不周全之处,请大家谅解。
例如:
class Student
{
    public:
            Show();
            ...
};
int main()
{
    Student *stu[100];//定义一个指针数组
    for(int i=0;i<100;i++)
    {
         stu[i]=new Student(...);
    }
    ShowAllStu(stu,100);//将stu作为参数传递
}
int ShowAllStu(Student * stu[],int size)//不知道形参形式是否正确,不过编译没问题
{
    for(int i=0;i<size;i++)
    {
         (*(stu[i])).Show();//不知这样调用对不对,当程序运行到这时会引起内存错误
    }   
}

3 回复
#2
makebest2010-08-20 23:41
没有经过验证, 但感觉应该这样:
stu[i]->show();
#3
xishui7772010-08-21 01:15
#include<iostream>
using namespace std;
class Student
{
    public:
           void Show(){};
};
int ShowAllStu(Student * stu[],int size)//不知道形参形式是否正确,不过编译没问题
{
    for(int i=0;i<size;i++)
    {
         (*(stu[i])).Show();//不知这样调用对不对,当程序运行到这时会引起内存错误
    }   
}
int main()
{
    Student *stu[100];//定义一个指针数组
    for(int i=0;i<100;i++)
    {
         stu[i]=new Student;
    }
    ShowAllStu(stu,100);//将stu作为参数传递
    system("pause");
    return 0;
}
是了得,可以过
1