473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why I can't change the gloable variable

#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.

Sep 11 '06 #1
13 1718
In article <11**********************@e3g2000cwe.googlegroups. com>,
Sakula <jx*******@gmail.comwrote:

>/* includes */
>#include<iostream.h>
That's a C++ program. Please try a C++ newsgroup. C++ is a different
language than C and has some important differences from C.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 11 '06 #2
Sakula wrote:
[...]
#include<iostream.h>
This header does not exist in either C or C++. The (offtopic) C++
language has a simliarly named <iostreamheader. Similar functionality
in the (topical) C language uses the <stdio.hheader.
#include<malloc.h>
This header does not exist in either C or C++. In the C language, the
only one topical here, malloc is prototyped in <stdlib.h>
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.
Sep 11 '06 #3
Walter Roberson said:
In article <11**********************@e3g2000cwe.googlegroups. com>,
Sakula <jx*******@gmail.comwrote:

>>/* includes */
>>#include<iostream.h>

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 11 '06 #4
In article <o4******************************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
>Walter Roberson said:
>In article <11**********************@e3g2000cwe.googlegroups. com>,
Sakula <jx*******@gmail.comwrote:
>>>/* includes */
>>>#include<iostream.h>
>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.
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 <<)
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Sep 11 '06 #5
Richard Heathfield wrote:
Walter Roberson said:
>Sakula <jx*******@gmail.comwrote:
>>/* includes */

#include<iostream.h>

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.
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com
Warning: Do not use Ultimate-Anonymity
They are worthless spammers that are running a scam.

Sep 11 '06 #6
Walter Roberson said:
In article <o4******************************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
>>Walter Roberson said:
>>In article <11**********************@e3g2000cwe.googlegroups. com>,
Sakula <jx*******@gmail.comwrote:
>>>>/* includes */
>>>>#include<iostream.h>
>>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.

It was a C++ program.
That's debatable, given the inclusion of <iostream.h>, which is not a
standard C++ header.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 11 '06 #7
CBFalconer said:
Richard Heathfield wrote:
>Walter Roberson said:
>>Sakula <jx*******@gmail.comwrote:

/* includes */

#include<iostream.h>

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.

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 11 '06 #8
"Sakula" <jx*******@gmail.comwrites:

[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).

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Sep 11 '06 #9
Richard Heathfield wrote:
CBFalconer said:
>Richard Heathfield wrote:
>>Walter Roberson said:
Sakula <jx*******@gmail.comwrote:

/* includes */
>
#include<iostream.h>

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.

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.
Which is quoted in its entirety above. I see nothing new.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
--
Posted via a free Usenet account from http://www.teranews.com
Warning: Do not use Ultimate-Anonymity
They are worthless spammers that are running a scam.

Sep 11 '06 #10
CBFalconer said:
Richard Heathfield wrote:
>CBFalconer said:
>>Richard Heathfield wrote:
Walter Roberson said:
Sakula <jx*******@gmail.comwrote:
>
>/* includes */
>>
>#include<iostream.h>
>
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.

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.

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".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 11 '06 #11
On Mon, 11 Sep 2006 13:32:29 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>
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
Sep 11 '06 #12
Mark McIntyre said:
On Mon, 11 Sep 2006 13:32:29 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>>
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
No such thing. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 11 '06 #13
On Mon, 11 Sep 2006 21:41:10 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>Mark McIntyre said:
>On Mon, 11 Sep 2006 13:32:29 +0000, in comp.lang.c , Richard
Heathfield <in*****@invalid.invalidwrote:
>>>
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

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
Sep 11 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
3
by: Byron | last post by:
Hi, Javascript confuses me, so I usually limit myself to Dreamweaver's built-in scripts for stuff like imageswaps. But this time I'm trying to write something very simple myself. I do most of my...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
5
by: Wing | last post by:
Hi all, I am writing a function that can change the value "Quantity" in the selected row of MS SQL table "shoppingCart", my code is showing below ...
5
by: Steven | last post by:
I'm using C# as code-behind for my aspx pages. How can I bind pages.. so that both the pages can use the functions, variables, controls .. between each other. eg., if I declare function scope()...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
5
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
13
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.