Re: [Dev-C++] A problem about the pointer.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Rafael C. <cha...@un...> - 2004-06-15 17:14:38
|
Well, when "Func" ends, its local variables (number)
are destroyed, so the result of printf shouldn't be
11 since "number" has been destroyed.
I tested your program and result was the same.
IMHO what may be happening is that your test program
is enough small to not modify the memory and
somehow pointer "p" is still pointing to a place
where the value is 11.
Change "main" as below and see the result.
In my test I had "x =3D 1" and "*p =3D 1".
main(){
int x =3D 1;
Func();
printf("x =3D %d\n", x);
printf("*p =3D %d\n", *p);
}
Maybe some other subscriber of this list could
give us a better answer. There are good programmers
here!
Hope it helps,
Rafael
Bruce jjs wrote:
> I have write a very simple program to test the pointer.The=20
> program is list belove:
> /* **************************************** */
> #include <stdio.h>
> =20
> int *p;
> =20
> =20
> void Func(void)
> {
> int number;
> =20
> number =3D 11;
> p =3D &number;
> }
> =20
> main()
> {
> Func();
> printf("*p =3D %d\n", *p);
> }
> /* ***************************** */
> The result is :
> *p =3D 11
> The variable "number" is local variable that will be=20
> destoryed after leave the Func( ) and the pointer "p" is pointer=20
> to it.Although p is global variable,it pointer to a deleted area. To my=
=20
> way of thinking,*p should be a ignorance value.Is my understand wrong?
> Thanks,
> Bruce.J
>=20
>=20
> -----------------------------------------------------------------------=
-
> Do You Yahoo!?
> =CF=D3=D3=CA=CF=E4=CC=AB=D0=A1=A3=BF=D1=C5=BB=A2=B5=E7=D3=CA=D7=D4=D6=FA=
=C0=A9=C8=DD=A3=A1=20
> <http://cn.rd.yahoo.com/mail_cn/tag/10m/*http://cn.mail.yahoo.com/event=
/10m.html>=20
>=20
|