pointer, pass reference variable into funtion

excuse me! who can fix code for me. i don't understand how my code not run.
thank you very much!

#include "stdafx.h"
#include<iostream>

using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
float a;
float hoa;
};
student *arr;
void input(student **arr,int spt){
arr = new student *[spt];
for(int i=0;i<spt;i++){
arr[i]= new student();
}
for(int i=0;i<spt;i++){
cout<<"the student: "<<i<<endl;
cout<<"enter name of student: "<<endl;
cin.getline(arr[i]->name,30);
}
}
void display(){
cout<<"the first student: "<<arr[0].name;
}
int _tmain(int argc, _TCHAR* argv[])
{

input(&arr,2);
display();
system("pause");
return 0;
}
Please, describe the "not run" in more detail.
And while you're at it, use code tags to make your code more readable.
Where do I start? I start by ignoring the numerous problems with this program, and why it will fail under many easily foreseeable circumstances.
Apologies if I seem to be bashing you, but really we could discuss the problems with this short piece of code for more than half a semester.

Also, please use the code-formatting tags, it makes it easier to read and comment on the code you post.

Primarily your use of the variable "arr" is confusing to say the least. You've declared a global variable, and are now passing the address of same variable to a function that can of course see the global definition, and then you use a local variable of the same name but different type (one is a pointer and the other is a pointer to pointer), so in your function input(), which "arr" is being used? "local student **arr" or "global student * arr"? You know which one you want it to be, but wishes aren't horses, which is probably why you're learning to program :)

In input(), you're filling up an array of pointers to student, but in display, you're accessing an array of objects, how can this work?

Je repete: there are too many fundamental issues to address here, but quick and dirty if you want to make this "work" is to do as shown below. Line numbers are approximate because someone didn't format their code :(

line 13: change from student *arr to student **arr
line 14: change from void input(student **arr,int spt) to void input(int spt)
line 26: change from cout<<"the first student: "<<arr[0].name to cout<<"the first student: "<<arr[0]->name
line 31: change from input(&arr,2) to input(2)

Also don't forget to delete [] arr when you're done in main()

Finally, don't be discouraged, keep practising, READING books, articles, etc. Carefully consider others comments however harsh and unjustifiable they may seem.
Topic archived. No new replies allowed.