Thread: [Dev-C++] Question about pointers (beginner)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Christian S. <sm...@xs...> - 2003-05-27 18:17:08
|
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
|
|
From: Christian S. <sm...@xs...> - 2003-05-27 18:27:23
|
I just executed this code:
#include <stdio.h>
main ( )
{
int x, *ptr_int;
*ptr_int = 456;
printf ("The value of ptr_int is %p\n",ptr_int);
printf ("The value of *ptr_int is %d\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);
}
And it gives me as expected 123 for x. However I would expect an error or
something like that for the line: *ptr_int = 456;
as *ptr_int is not linked to any memory address at that time. Anyway maybe
it was a error in the book when they stated that the end result of x should
be 456 instead of 123 as I expected.
|
|
From: <or...@vp...> - 2003-05-27 19:34:13
|
Christian Schmidt wrote:
> Given x = 123 and ptr_int = &x after that *ptr_int = 456 has been executed.
> What is the value of x?
If I implemented this, this is how I would do it:
#include <stdio.h>
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? */
/* The answer is: 123 */
}
> 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 */
>
> }
This is a *wrong* implementation of the example. The book does not state
this. It states what I wrote above.
And the reason why it does not segfault is only luck. *ptr_int is not
NULL at the beginning! Variables are NOT initialised, it is up to you to
do that. *ptr_int just points to somewhere, and you only had luck, that
using it didn't cause any trouble. Try setting ptr_int to NULL before
you dereference it (*ptr_int). It will surely segfault then.
You should always keep it in mind, that variables are NEVER initialised
to a default value! You must have to do that!
> But the books says the value of *ptr_int at the end is 456. How come??
The book is correct.
--
Greetings,
Balázs
|
|
From: Smurf, P. <smu...@xs...> - 2003-05-27 19:55:05
|
You are correct I misread it.
Thanks
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of OROSZI
> Bal=E1zs
> Sent: dinsdag 27 mei 2003 21:35
> To: dev...@li...
> Subject: Re: [Dev-C++] Question about pointers (beginner)
>
>
> Christian Schmidt wrote:
> > Given x =3D 123 and ptr_int =3D &x after that *ptr_int =3D 456 has
> been executed.
> > What is the value of x?
>
> If I implemented this, this is how I would do it:
>
> #include <stdio.h>
>
> main()
> {
> int x, *ptr_int;
>
> x =3D 123; /* Given x =3D 123 */
> ptr_int =3D &x; /* and ptr_int =3D&x */
> *ptr_int =3D 456; /* after that *ptr_int =3D 456 has been executed =
*/
> printf("%d",x); /* What is the value of x? */
> /* The answer is: 123 */
> }
>
> > This is how I see it:
> >
> > main ( )
> > {
> > int x, *ptr_int;
> >
> > *ptr_int =3D 456; /*This pointer is a null pointer at this moment
> right? */
> >
> > x =3D123;
> > ptr_int =3D &x; /* As x is just set to 123 and the statement
> *ptr_int =3D 456;
> > was executed before I would think *ptr_int would be 123 */
> >
> > }
>
> This is a *wrong* implementation of the example. The book does not stat=
e
> this. It states what I wrote above.
>
> And the reason why it does not segfault is only luck. *ptr_int is not
> NULL at the beginning! Variables are NOT initialised, it is up to you t=
o
> do that. *ptr_int just points to somewhere, and you only had luck, that
> using it didn't cause any trouble. Try setting ptr_int to NULL before
> you dereference it (*ptr_int). It will surely segfault then.
> You should always keep it in mind, that variables are NEVER initialised
> to a default value! You must have to do that!
>
> > But the books says the value of *ptr_int at the end is 456. How come?=
?
>
> The book is correct.
>
> --
> Greetings,
> Bal=E1zs
>
>
>
> -------------------------------------------------------
> 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: Ioannis V. <iv...@te...> - 2003-05-27 21:05:05
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Christian Schmidt > Sent: Tuesday, May 27, 2003 9:18 PM > To: dev...@li... > Subject: [Dev-C++] Question about pointers (beginner) > > > >From my book: > > Given x = 123 and ptr_int = &x Now ptr_int has the address of x stored. > after that *ptr_int = 456 has *ptr_int accesses the value of x, so *ptr_int=456 is equivalent to x=456. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |