Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   *pt1++ givung wrong value (http://www.velocityreviews.com/forums/t453609-pt1-givung-wrong-value.html)

pxe 04-26-2006 07:14 AM

*pt1++ givung wrong value
 
Hi,

Please let me know what is wrong with my code below. *pt1++ is giving
me 10.
Is it not valid?


#include<iostream>
#include<stdlib.h>


using namespace std;


int main(int argc,char *argv[])
{
int a=0;


int *pt1;
int *pt2;
float *pt3;


int values[100];


int *gels[100];


pt1=&a;
cout<<*pt1;
*pt1++;
a=*pt1;
cout<<a<<*pt1;


return 0;



}


Thanks and Regards
Babu


Ivan.Filimonov@gmail.com 04-26-2006 07:49 AM

Re: *pt1++ givung wrong value
 
Hi,

If you want to increment "a" try (*pt1)++.


Zara 04-26-2006 09:29 AM

Re: *pt1++ givung wrong value
 
On 26 Apr 2006 00:14:23 -0700, "pxe" <babu.kaipa@gmail.com> wrote:

>Hi,
>
>Please let me know what is wrong with my code below. *pt1++ is giving
>me 10.
>Is it not valid?


Its results are undefined, see comments below
>
>
>#include<iostream>
>#include<stdlib.h>
>
>
>using namespace std;
>
>
>int main(int argc,char *argv[])
>{
>int a=0;
>
>
>int *pt1;
>int *pt2;
>float *pt3;
>
>
>int values[100];
>
>
>int *gels[100];
>
>
>pt1=&a;
>cout<<*pt1;
>*pt1++;


This does nothing woth the value pointed by pt1 and increments the
pointer

>a=*pt1;

A is assigned the value of undefined memory.

>cout<<a<<*pt1;
>
>
>return 0;
>
>
>
>}



Best regards,

Zara

Salt_Peter 04-26-2006 10:15 AM

Re: *pt1++ givung wrong value
 

"pxe" <babu.kaipa@gmail.com> wrote in message
news:1146035663.272693.94520@g10g2000cwb.googlegro ups.com...
> Hi,
>
> Please let me know what is wrong with my code below. *pt1++ is giving
> me 10.
> Is it not valid?
>
>
> #include<iostream>
> #include<stdlib.h>
>
>
> using namespace std;
>
>
> int main(int argc,char *argv[])
> {
> int a=0;
>
>
> int *pt1;
> int *pt2;
> float *pt3;
>
>
> int values[100];
>
>
> int *gels[100];
>
>
> pt1=&a;
> cout<<*pt1;
> *pt1++;


translation: increment the pointer to integer so that pt1 points to pure
garbage.

> a=*pt1;


load variable a with whatever garbage was found there.

> cout<<a<<*pt1;


print the garbage twice.

>
>
> return 0;
>
>
>
> }
>
>
> Thanks and Regards
> Babu
>




Jim Langston 04-27-2006 12:40 AM

Re: *pt1++ givung wrong value
 
Comments inline. Code that doesn't apply snipped.

"pxe" <babu.kaipa@gmail.com> wrote in message
news:1146035663.272693.94520@g10g2000cwb.googlegro ups.com...
> Hi,
>
> Please let me know what is wrong with my code below. *pt1++ is giving
> me 10.
> Is it not valid?
>
> #include<iostream>
> #include<stdlib.h>
>
> using namespace std;
>
> int main(int argc,char *argv[])
> {
> int a=0;
>
> int *pt1;
>
> pt1=&a;


pt1 now points to the memory used to store the integer a.

> cout<<*pt1;


You are displaying the contents of the memory used to store the integer a

> *pt1++;


You are incrementing the pointer. pt1 now points sizeof(int) bytes paste
the memory location used to store the integer a. Ooops. ++ takes precidence
over *. This means it evaluates to *(pt1++) incrementing the pointer then
taking the value, which nothing is done with. You need to manually set
precidence. Change this to (*pt1)++;

> a=*pt1;


a now contains the contents of the memory sizeof(int) bytes past where a was
stored because of the above line.

> cout<<a<<*pt1;


This should display the same (garbage) value twice.

> return 0;
>
> }





All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.