*pt1++

  • Thread starter Thread starter pxe
  • Start date Start date
P

pxe

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
 
First of all, I am not sure what you are trying to do. Since pt1 actually
points to a, doing a = *pt1 sets a to itself.

If you are tring to increment the value pointed to by pt1 then you should do
(*pt1)++ instead of *pt1++, otherwise operator precedence will increment the
actual pointer pt1 to point to 4 bytes past where a is stored.
 
This isn't really the right group for you to ask this, you'd be better
off in
comp.lang.c++

However, you might want to try changing:

*pt1++ to (*pt1)++

You are incrementing the pointer, not the value stored there.

Glyn
 
pxe said:
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

1. This newsgroup isn't for helping people with coding problems - it's for
helping with the XP Embedded operating system and issues building it.
2. You may be confused about operator precedence (++ goes before *). See
http://www.cppreference.com/operator_precedence.html.
 
Back
Top