*pt1++ givung wrong value

Discussion in 'C++' started by pxe, Apr 26, 2006.

  1. pxe

    pxe Guest

    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
     
    pxe, Apr 26, 2006
    #1
    1. Advertising

  2. pxe

    Guest

    Re: *pt1++ givung wrong value

    Hi,

    If you want to increment "a" try (*pt1)++.
     
    , Apr 26, 2006
    #2
    1. Advertising

  3. pxe

    Zara Guest

    On 26 Apr 2006 00:14:23 -0700, "pxe" <> 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
     
    Zara, Apr 26, 2006
    #3
  4. pxe

    Salt_Peter Guest

    "pxe" <> wrote in message
    news:...
    > 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
    >
     
    Salt_Peter, Apr 26, 2006
    #4
  5. pxe

    Jim Langston Guest

    Comments inline. Code that doesn't apply snipped.

    "pxe" <> wrote in message
    news:...
    > 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;
    >
    > }
     
    Jim Langston, Apr 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Reinemann

    PT1 in VHDL

    Thomas Reinemann, Oct 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    627
    mike_treseler
    Oct 27, 2004
  2. Paul
    Replies:
    1
    Views:
    630
    Kris van der Mast
    Aug 27, 2003
  3. Stephan

    e.Item.ItemIndex returns wrong value

    Stephan, Jul 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,499
    Stephan Bussing
    Jul 26, 2004
  4. gce
    Replies:
    0
    Views:
    796
  5. Matthew
    Replies:
    7
    Views:
    875
    Priscilla Walmsley
    Jan 7, 2005
Loading...

Share This Page