Thread: Re: [Dev-C++] Question about pointers (beginner)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Khalid El-K. <kha...@ho...> - 2003-05-27 19:13:59
|
Did you try that code with any compiler, the line *ptr_int = 456; should
give a segmentation fault!
given that there's isn't a code you missed from the book :-)
>From my book:
>
>Given x = 123 and ptr_int = &x after that *ptr_int = 456 has been executed.
>What is the value of x?
>
>I thought it was 123 as the pointer *ptr_int is a null pointer and can't
>have any value.
>
>This is how I see it:
>
>main ( )
>{
>int x, *ptr_int;
>
>*ptr_int = 456; /*This pointer is a null pointer at this moment right? */
>
>x =123;
>ptr_int = &x; /* As x is just set to 123 and the statement *ptr_int = 456;
>was executed before I would think *ptr_int would be 123 */
>
>}
>
>----------
>
>But the books says the value of *ptr_int at the end is 456. How come??
>
>Thanks for your time,
>
>Christian
>
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: ObjectStore.
>If flattening out C++ or Java code to make your application fit in a
>relational database is painful, don't do it! Check out ObjectStore.
>Now part of Progress Software. http://www.objectstore.net/sourceforge
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
|
|
From: <or...@vp...> - 2003-05-27 21:00:20
|
Avik Ghose wrote:
> I think still x will be 456. Lets look at it this way.
> x=123.
> p=&x
> now *p implies the content of the address pointed to by p which is
> nothing but the memory symbolised by the name 'x'.
> hence when *p=456 is set, after that.
> x will also be equal to 456
WAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!
ME FOOOOOOOOOOOOL!!!
It is a serious TYPO!!!!
I thought 456, and I wrote 123!
Stupid, fool, idiot...
I'll correct it:
If I implemented this, this is how I would do it:
#include
main()
{
int x, *ptr_int;
x = 123; /* Given x = 123 */
ptr_int = &x; /* and ptr_int =&x */
*ptr_int = 456; /* after that *ptr_int = 456 has been executed */
printf("%d",x); /* What is the value of x? */
/* ***CORRECTED***: THE ANSWER IS: 456 */
}
Sorry, and thanks.
It was a typo. My brain sometimes is out %)
--
Greetings,
Balázs
|
|
From: <a_g...@ya...> - 2003-05-27 22:37:47
|
Mention not friend, it happens to everybody sometimes :-).
OROSZI_Balázs <or...@vp...> wrote:Avik Ghose wrote:
> I think still x will be 456. Lets look at it this way.
> x=123.
> p=&x
> now *p implies the content of the address pointed to by p which is
> nothing but the memory symbolised by the name 'x'.
> hence when *p=456 is set, after that.
> x will also be equal to 456
WAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!
ME FOOOOOOOOOOOOL!!!
It is a serious TYPO!!!!
I thought 456, and I wrote 123!
Stupid, fool, idiot...
I'll correct it:
If I implemented this, this is how I would do it:
#include
main()
{
int x, *ptr_int;
x = 123; /* Given x = 123 */
ptr_int = &x; /* and ptr_int =&x */
*ptr_int = 456; /* after that *ptr_int = 456 has been executed */
printf("%d",x); /* What is the value of x? */
/* ***CORRECTED***: THE ANSWER IS: 456 */
}
Sorry, and thanks.
It was a typo. My brain sometimes is out %)
--
Greetings,
Balázs
-------------------------------------------------------
This SF.net email is sponsored by: ObjectStore.
If flattening out C++ or Java code to make your application fit in a
relational database is painful, don't do it! Check out ObjectStore.
Now part of Progress Software. http://www.objectstore.net/sourceforge
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Thanx and Regards
Avik Ghose
Catch all the cricket action. Download Yahoo! Score tracker |
|
From: Christian S. <sm...@xs...> - 2003-05-27 19:38:09
|
I tried the code below with a standard DevCPP and MSVC++.
DevCPP just compiles and runs it and gives 456 for *ptr_int even before its
initialized.
MSVC++ 6 compiles it but gives an warning that the pointer hasn't been
initialized. When I try to run it, it crashes.
The code:
#include <stdio.h>
main ( )
{
int x, *ptr_int;
*ptr_int = 456;
printf ("The value of ptr_int is %d\n",ptr_int);
printf ("The value of ptr_int is %p\n",ptr_int);
printf ("The value of *ptr_int is %d\n",*ptr_int);
printf ("The value of &ptr_int is %p\n",&ptr_int);
x = 123;
ptr_int = &x;
printf ("The value of ptr_int is %p\n",ptr_int);
printf ("The value of *ptr_int is %d\n",*ptr_int);
printf ("The value of x is %d\n",x);
printf ("The value of &x is %p\n",&x);
}
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Khalid
> El-Kary
> Sent: dinsdag 27 mei 2003 21:14
> To: Dev...@li...
> Subject: Re: [Dev-C++] Question about pointers (beginner)
>
>
> Did you try that code with any compiler, the line *ptr_int = 456; should
> give a segmentation fault!
> given that there's isn't a code you missed from the book :-)
>
> >From my book:
> >
> >Given x = 123 and ptr_int = &x after that *ptr_int = 456 has
> been executed.
> >What is the value of x?
> >
> >I thought it was 123 as the pointer *ptr_int is a null pointer and can't
> >have any value.
> >
> >This is how I see it:
> >
> >main ( )
> >{
> >int x, *ptr_int;
> >
> >*ptr_int = 456; /*This pointer is a null pointer at this moment right? */
> >
> >x =123;
> >ptr_int = &x; /* As x is just set to 123 and the statement
> *ptr_int = 456;
> >was executed before I would think *ptr_int would be 123 */
> >
> >}
> >
> >----------
> >
> >But the books says the value of *ptr_int at the end is 456. How come??
> >
> >Thanks for your time,
> >
> >Christian
> >
> >
> >
> >-------------------------------------------------------
> >This SF.net email is sponsored by: ObjectStore.
> >If flattening out C++ or Java code to make your application fit in a
> >relational database is painful, don't do it! Check out ObjectStore.
> >Now part of Progress Software. http://www.objectstore.net/sourceforge
> >_______________________________________________
> >Dev-cpp-users mailing list
> >Dev...@li...
> >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
> _________________________________________________________________
> Protect your PC - get McAfee.com VirusScan Online
> http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ObjectStore.
> If flattening out C++ or Java code to make your application fit in a
> relational database is painful, don't do it! Check out ObjectStore.
> Now part of Progress Software. http://www.objectstore.net/sourceforge
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2003-05-27 22:41:12
|
Since ptr_int is unitialized at the first assign (*ptr_int = 456), it is
completely random - ur op to the compiler - to decide what happens.
In most cases, the ptr_int will be piont at whatever value happens to be
stored at that location in the stack when main() is called. For some
compilers - such as MSVC - a compile build might initialize auto
(stack-based) variables to magic values, just to more easily follow and
detect these magic values at other parts of the program. It works
reasonably well but not perfect since the magic values are not reserved,
but normal integer values and might be used for real in some programs.
Because ptr_int is uninitialized, sometimes the program works, sometimes
it gives wrong answers, sometimes it explodes big time.
/Per W
On Tue, 27 May 2003, Christian Schmidt wrote:
> I tried the code below with a standard DevCPP and MSVC++.
>
> DevCPP just compiles and runs it and gives 456 for *ptr_int even before its
> initialized.
> MSVC++ 6 compiles it but gives an warning that the pointer hasn't been
> initialized. When I try to run it, it crashes.
>
> The code:
>
> #include <stdio.h>
>
> main ( )
> {
> int x, *ptr_int;
>
> *ptr_int = 456;
>
> printf ("The value of ptr_int is %d\n",ptr_int);
> printf ("The value of ptr_int is %p\n",ptr_int);
> printf ("The value of *ptr_int is %d\n",*ptr_int);
> printf ("The value of &ptr_int is %p\n",&ptr_int);
>
> x = 123;
>
> ptr_int = &x;
>
> printf ("The value of ptr_int is %p\n",ptr_int);
> printf ("The value of *ptr_int is %d\n",*ptr_int);
> printf ("The value of x is %d\n",x);
> printf ("The value of &x is %p\n",&x);
>
> }
>
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...]On Behalf Of Khalid
> > El-Kary
> > Sent: dinsdag 27 mei 2003 21:14
> > To: Dev...@li...
> > Subject: Re: [Dev-C++] Question about pointers (beginner)
> >
> >
> > Did you try that code with any compiler, the line *ptr_int = 456; should
> > give a segmentation fault!
> > given that there's isn't a code you missed from the book :-)
> >
> > >From my book:
> > >
> > >Given x = 123 and ptr_int = &x after that *ptr_int = 456 has
> > been executed.
> > >What is the value of x?
> > >
> > >I thought it was 123 as the pointer *ptr_int is a null pointer and can't
> > >have any value.
> > >
> > >This is how I see it:
> > >
> > >main ( )
> > >{
> > >int x, *ptr_int;
> > >
> > >*ptr_int = 456; /*This pointer is a null pointer at this moment right? */
> > >
> > >x =123;
> > >ptr_int = &x; /* As x is just set to 123 and the statement
> > *ptr_int = 456;
> > >was executed before I would think *ptr_int would be 123 */
> > >
> > >}
> > >
> > >----------
> > >
> > >But the books says the value of *ptr_int at the end is 456. How come??
> > >
> > >Thanks for your time,
> > >
> > >Christian
> > >
> > >
> > >
> > >-------------------------------------------------------
> > >This SF.net email is sponsored by: ObjectStore.
> > >If flattening out C++ or Java code to make your application fit in a
> > >relational database is painful, don't do it! Check out ObjectStore.
> > >Now part of Progress Software. http://www.objectstore.net/sourceforge
> > >_______________________________________________
> > >Dev-cpp-users mailing list
> > >Dev...@li...
> > >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> > _________________________________________________________________
> > Protect your PC - get McAfee.com VirusScan Online
> > http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
> >
> >
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: ObjectStore.
> > If flattening out C++ or Java code to make your application fit in a
> > relational database is painful, don't do it! Check out ObjectStore.
> > Now part of Progress Software. http://www.objectstore.net/sourceforge
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ObjectStore.
> If flattening out C++ or Java code to make your application fit in a
> relational database is painful, don't do it! Check out ObjectStore.
> Now part of Progress Software. http://www.objectstore.net/sourceforge
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|