why I can't change the gloable variable

S

Sakula

#define MAX 20

/* includes */


#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>


struct Student
{
int number;
int age;
int male;
char name[20];


Student * next;



};


Student * Create();
void ShowList(Student * g_head);
Student * Delete(Student * g_head, int age);

Student * g_head;


Student * Create()
{


Student * pS;
Student * pEnd;


pS=new Student;
cout<<"please input number,age ,male and name"<<endl;
cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);


g_head=NULL;
pEnd=pS;


while(pS->number!=0)
{
if(g_head==NULL)
g_head = pS;
else
pEnd->next=pS;


pEnd=pS;
pS=new Student;
cout<<"please input number,age , male and name"<<endl;

cin>>pS->number>>pS->age>>pS->male;
gets(pS->name);
}
pEnd->next=NULL;
delete pS;
return (g_head);



}


void ShowList(Student * g_head)
{
cout<<"now the items of list are \n";

while(g_head)
{
cout<<g_head->number<<" "<<g_head->age<<"
"<<g_head->male<<"
"<<g_head->name<<endl;
g_head=g_head->next;
}



}


Student * Delete(Student * g_head, int age)
{
Student *p=NULL;
p=new Student;
if(!g_head)
{
cout<<"\nList null!\n";
return NULL;
}
if(g_head->age==age)
{
p=g_head;
g_head=g_head->next;
delete p;
cout<<age<<"the g_head of list has been deleted\n";
return g_head;
}
for(Student * pGuard=g_head; pGuard->next;pGuard=pGuard->next)
{
if(pGuard->next->age==age)
{
p=pGuard->next;
pGuard->next=p->next;
delete p;
cout<<age<<"has been deleted\n";
return g_head;
}
}

cout<<age<<"is not found! \n";



}


void main()
{
int age=0;

g_head=Create();
cout<<"now the list are \n";

ShowList(g_head);
cout<<"please input the age you want to delete : \n";
cin>>age;

Delete(g_head,age);

ShowList(g_head);


}


when i delete the head of the linked list,it will be crupt!
I debug this program and find the gloable variable g_head doesn't be
changed by Delete( ). I don't know why this happen.
 
M

Martin Ambuhl

Sakula wrote:
[...]
#include<iostream.h>

This header does not exist in either C or C++. The (offtopic) C++
language has a simliarly named <iostream> header. Similar functionality
in the (topical) C language uses the said:
#include<malloc.h>
This header does not exist in either C or C++. In the C language, the
only one topical here said:
struct Student
{
int number;
int age;
int male;
char name[20];
Student * next;
^^^^^^^
This is not legal in C without a prior typedef.
};


Student * Create();
^^^^^^^
This is not legal in C without a prior typedef.
[...]

void main()
^^^^
This is not legal in C or C++.

etc.

You are clearly lost. Not only is your code not C, which would be
topical in comp.lang.c, you have done the nearly impossible: written
gobbledygook that not even a (non-topical) C++ compiler would accept as
standard C++.

If you want to post C++ questions, do so in the appropriate newsgroup.
But learn something about C++ first: if you can't even write a legal
definition for main, it's time for you to start over at page 1.
 
R

Richard Heathfield

Walter Roberson said:
That's a C++ program.

You may be right (although in fact you probably are not), but the evidence
cited above is insufficient to demonstrate this. C implementations are free
to ship headers by that name.
 
W

Walter Roberson

Walter Roberson said:
You may be right (although in fact you probably are not), but the evidence
cited above is insufficient to demonstrate this. C implementations are free
to ship headers by that name.

It was a C++ program. It had numerous cout uses, including at least
one cout <<" " which is not valid in C (wrong type of argument for <<)
 
C

CBFalconer

Richard said:
Walter Roberson said:

You may be right (although in fact you probably are not), but the
evidence cited above is insufficient to demonstrate this. C
implementations are free to ship headers by that name.

Well, later on he is doing such things as calling an undeclared
function 'new', and doing shifts into the undeclared variable
cout. You may want to reconsider your probabilities.
 
R

Richard Heathfield

CBFalconer said:
Well, later on he is doing such things as calling an undeclared
function 'new', and doing shifts into the undeclared variable
cout. You may want to reconsider your probabilities.

You may want to re-read my reply, and especially the first sentence thereof.
 
M

Michal Nazarewicz

[cut]
Student * g_head; [cut]
Student * Delete(Student *g_head, int age) { [cut]
}

void main() { [cut]
Delete(g_head, age); [cut]

when i delete the head of the linked list,it will be crupt!
I debug this program and find the gloable variable g_head doesn't be
changed by Delete( ). I don't know why this happen.

That's because Delete() has it's own g_head variable passed as an
argument which /shadows/ the global g_head variable. If you want to
be able to modify passed argument you'll have to change definition of
Delete to Student *Delete(Student **g_head, int age) and change the
program approprietly.

BTW. your post doesn't belong here as you are writting in C++ not
C (the "new" operator revelas that).
 
R

Richard Heathfield

CBFalconer said:
Which is quoted in its entirety above. I see nothing new.

Your argument rests on "later on he is doing such things as...", whereas my
statement only referred to "the evidence cited above".
 
M

Mark McIntyre

Your argument rests on "later on he is doing such things as...", whereas my
statement only referred to "the evidence cited above".

Then its a nit you should not have picked, it just makes you look anal
about the wrong things.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Mark McIntyre said:


No such thing. :)

I'll just cry "emerson" and pass on.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top