A 21-year-old student killed in a shooting at the University of North Carolina at Charlotte saved some of his classmates' lives by charging the gunman and attempting to disarm him, the city's top police official has said.

Environmental studies student Riley Howell of Waynesville, North Carolina, one of two students killed in the shooting yesterday, played a key role in ending the attack by a former student, said Kerr Putney, chief of the Charlotte-Mecklenburg Police Department.

"But for his work, the assailant may not have been disarmed," Mr Putney told a news conference.

"He's an athletically built young man and he took the fight to the assailant. Unfortunately he had to give his life to do so," said Mr Putney, a UNC Charlotte alumnus.

"He took the assailant off his feet."

The other student killed in the shooting was 19-year-old Ellis Parlier, officials said.

Four wounded students were identified as Drew Pescaro, 19; Sean DeHart, 20; Emily Houpt, 23; and Rami Alramadhan, 20.

Police in Charlotte arrested former UNC Charlotte student Trystan Andrew Terrell, 22, on suspicion of murder and attempted murder but offered no explanation for a motive. They said he used a legally purchased handgun and was carrying a large amount of ammunition.

Terrell was familiar with the classroom building where the attack occurred but it was unclear if he knew the students targeted, Mr Putney said.

"We can't really discern the why just yet," Mr Putney said."The randomness is what is most concerning."

He added that police believe Terrell acted alone.

Terrell is due to make an initial court appearance tomorrow, jail records show.

The school's chancellor, Phil Dubois, said all of the wounded were expected to recover, with one already released fromt he hospital.

Ms Houpt is due to graduate from the school later this month, Mr Dubois said, adding that the school's commencement ceremonies would go on as scheduled next week.

Final exams have been postponed and the school will consider allowing professors to cancel them, he said.

Students spent a harrowing night as police searched door-to-door for other suspects, not giving the all-clear until almost dawn.

The shooting started in a classroom at about 5.40pm, police said.

Tristan Field, a student who witnessed the shooting, told CBS News as many as 50 students tried to escape through two doors.

"A chair fell in front of the door, so people were tripping over that, like, trying to climb over it," he said.

"Some people fell down. It was like water through a funnel but wasn't fast enough."

A vigil was planned at the school for Wednesday evening.

"When we are faced with something like this, I can't tell you the feelings, the sorrow," said Jeffrey Baker, chief of the university police.

The campus has about 30,000 students and 5,000 faculty and staff, officials said.