...

View Full Version : null pointer assignment



Aymen++
04-26-2004, 10:09 AM
when i run this program it run correctly but in the end it tells me "null pointer assinmebt" what does it mean?


#include <iostream.h>

struct student {
char *name;
float grade;
};

void main() {
student *s;
float sum=0;
float class_grade;
int n;
cout <<"Enter students number (less or equal 10): ";
cin >> n;
s = new student[n];

for(int i=0; i<n; i++) {
cout <<"\nEnter student's name: ";
cin >> s[i].name;
cout <<"\nEnter student's grade: ";
cin >> s[i].grade;
sum = sum + s[i].grade;
}
class_grade = sum / n;
cout <<"\nThe class grade is: "<<class_grade;

cout <<"\nThe students with grade less than the class grade are: ";
for(i=0; i<n; i++) {
if(s[i].grade<class_grade)
cout <<s[i].name<<"\n";
}

cout <<"The students with grade more than the class grade are:\n ";
for(i=0; i<n; i++) {
if(s[i].grade>=class_grade)
cout <<s[i].name<<"\n";
}
}

Mhtml
04-26-2004, 02:25 PM
It means you're assigning a value through a pointer that points to nothing ie -- a NULL pointer. :)

Unit
04-26-2004, 06:35 PM
You defined student.name as a pointer

struct student {
char *name;
float grade;
};
and you are trying to read a string into it without allocating memory!

for(int i=0; i<n; i++) {
cout <<"\nEnter student's name: ";
cin >> s[i].name;
You have allocated memory to the student structure itself. not the name member of it. The result of this would vary a lot from crash right at the start to perfect function of it if you get lucky.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum