C++ Visual 6.0关于New在函数中的用法

# include <iostream.h>
# include <string.h>
struct StudentCourse
{
int Grade;
int CourseOrder;
};
StudentCourse *tmp;
void fun(StudentCourse *s)
{
static int n=0;
n++;
StudentCourse *tmp=new StudentCourse[n];
if(tmp)
{
if((n-1)!=0)
{
for(int i(0);i<n;i++)
tmp[i].CourseOrder=i+1;
tmp[n-1].CourseOrder=n;
delete[] s;
s=tmp;
}
else
{
tmp[n-1].CourseOrder=n;
s=tmp;
}
}
else
cout<<"error!"<<endl;
}
void main()
{
StudentCourse *m;
for(int i(0);i<5;i++)
fun(m);
for(i=0;i<5;i++)
cout<<m[i].CourseOrder<<" ";
cout<<endl;

}

这样能编译过:

# include <iostream>
# include <string>
using namespace std;

struct StudentCourse
{
int Grade;
int CourseOrder;
};

void fun(StudentCourse *&s)
{
static int n = 0;
n++;
StudentCourse *tmp = new StudentCourse[n];
if (tmp)
{
if ((n - 1) != 0)
{
for (int i(0); i 轿链肢< n; i++)
tmp[i].CourseOrder = i + 1;
tmp[n - 1].CourseOrder = n;
delete[] s;
s = tmp;
}
else
{
tmp[n - 1].CourseOrder = n;
s = tmp;
}
}
else
cout << "error!" <闭世< endl;
}
void main()
{
StudentCourse *m = NULL/*nullptr*/;
int i(0);
for (; i < 5; i++)
fun(m);
for (i = 0; i < 5; i++)
cout << m[i].CourseOrder << "  ";
cout <<唤清 endl;
}