Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple pointer question

Reply
Thread Tools

simple pointer question

 
 
lokman
Guest
Posts: n/a
 
      04-17-2004
Hi,

In the following code, can someone tell me the difference between *p++ and
p++ ? I can see both achieve the same result.

Thanks a lot !




#include <iostream>
using namespace::std;
int main() {
char *p = "test pointer";
while (*p) {
cout << *p;
*p++; // <<- What is the difference between *p++ and p++ (both achieve
the same result) ?
}
cout << endl;
return 0;
}


 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      04-17-2004
"lokman" <(E-Mail Removed)> wrote in message
news:c5rdrn$a7h$(E-Mail Removed)...

> *p++; // <<- What is the difference between *p++ and p++ (both achieve
> the same result) ?


p ++ increments the pointer, like you expect. *p ++ first dereferences
the pointer, and then increments it. Since the dereferenced temporary is not
assigned to anything, it is discarded.

hth
--
jb

(replace y with x if you want to reply by e-mail)


 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      04-17-2004
On Sat, 17 Apr 2004 23:10:00 +0900, "lokman" <(E-Mail Removed)> wrote:

>>Hi,
>>
>>In the following code, can someone tell me the difference between *p++ and
>>p++ ? I can see both achieve the same result.
>>
>>Thanks a lot !
>>
>>
>>
>>
>>#include <iostream>
>>using namespace::std;
>>int main() {
>>char *p = "test pointer";
>>while (*p) {
>> cout << *p;
>> *p++; // <<- What is the difference between *p++ and p++ (both achieve
>>the same result) ?
>>}
>>cout << endl;
>>return 0;
>>}
>>


in this context there is no diff.

if you would for instance do a cout << *p++
then it would print p's current char and then
incr the ptr

*p get the value from the ptr
++ incr the ptr with whatever size of type it is.

/ak
 
Reply With Quote
 
Frane Roje
Guest
Posts: n/a
 
      04-17-2004
"lokman" <(E-Mail Removed)> wrote in message
news:c5rdrn$a7h$(E-Mail Removed)...
> Hi,
>
> In the following code, can someone tell me the difference between *p++ and
> p++ ? I can see both achieve the same result.
>
> Thanks a lot !


Ok,
p++ increments the pointer
*p gets the contents of the variable to which the pointer is pointing to

You could write in your loop
cout<<*p++<<endl;

Because of the precedence of the * operator so the *p is done before p++,
maybe it would be more clear if you wrote (*p)++ (don't use this in coding
this is just for you to understand you should write *p++ in your code)

Maybe an example of copying two strings;

char *s1="test";
char *s2="blab";

for(unsigned int i=0;i<strlen(s1);i++)
*s1++=*s2++;

So the content of the s1 becomes the content of the s2;
Note that they are both the same size, if not you might have
undefined behavior.
But if you want to copy strign I would reccomed using <string>
rather than char*.

HTH


--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-17-2004
lokman wrote:

> Hi,
>
> In the following code, can someone tell me the difference between *p++ and
> p++ ? I can see both achieve the same result.
>
> Thanks a lot !
>
>
> #include <iostream>
> using namespace::std;
> int main() {
> char *p = "test pointer";


Quick note: don't do this. Never make a (non-const) char pointer point
to a string literal. This conversion is allowed for C compatibility, but
is deprecated because it is dangerous. It allows you to write code that
(attempts to) modify a string literal without getting a warning from the
compiler. Modifying a string literal (or attempting to) invokes
undefined behavior.

If you want a pointer to a string literal, always use a pointer to const
char, like one of the following:

const char *p = "some string";
char const *p = "some string"; // same as prev

const char * const p = "some string";
char const * const p = "some string"; // same as prev

In the last two 'p' itself is also const.

> while (*p) {
> cout << *p;
> *p++; // <<- What is the difference between *p++ and p++ (both achieve
> the same result) ?
> }
> cout << endl;
> return 0;
> }
>
>


It looks like all the replies so far are wrong in one way or another.
The expression 'p++' causes p to be incremented at some point before the
next sequence point. It also has a result, which is the value of 'p'
before being incremented. There's an subtle but important point here:
you don't know *when* 'p' will actually be updated, only that it will
happen sometime before the next sequence point (usually a semi-colon,
but there are others). People frequently get this wrong.

As for the expression '*p++', it is equivalent to '*(p++)' (contrary to
what one of the other replies said, post-increment has higher precedence
than dereference -- check any precedence chart). So, as described
previously, 'p' is scheduled to be incremented at some point before the
next sequence point, and also a result is given. The result is the value
of 'p' prior to the increment. The '*' is applied to that, giving the
object that 'p' pointed to prior to the increment. This result is
immediately discarded in your case.

So there's no reason for the '*' in this case. It might even slow your
program down a little, so get rid of it.

Also, replace 'p++' with '++p'. Get in the habit of using pre-increment
in cases where either will work. There's a chance it will be faster, and
it almost certainly won't be slower. There probably is no difference for
built-in types, but for class types there may be a substantial difference.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-17-2004
Jakob Bieling wrote:

> "lokman" <(E-Mail Removed)> wrote in message
> news:c5rdrn$a7h$(E-Mail Removed)...
>
>
>> *p++; // <<- What is the difference between *p++ and p++ (both achieve
>>the same result) ?

>
>
> p ++ increments the pointer, like you expect. *p ++ first dereferences
> the pointer, and then increments it.


Nitpick: That sequence of events is not required.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-17-2004
Frane Roje wrote:

> "lokman" <(E-Mail Removed)> wrote in message
> news:c5rdrn$a7h$(E-Mail Removed)...
>
>>Hi,
>>
>>In the following code, can someone tell me the difference between *p++ and
>>p++ ? I can see both achieve the same result.
>>
>>Thanks a lot !

>
>
> Ok,
> p++ increments the pointer
> *p gets the contents of the variable to which the pointer is pointing to


Actually it gets the variable itself. "Contents of" sounds like a copy.

>
> You could write in your loop
> cout<<*p++<<endl;
>
> Because of the precedence of the * operator so the *p is done before p++,
> maybe it would be more clear if you wrote (*p)++ (don't use this in coding
> this is just for you to understand you should write *p++ in your code)


(*p)++ has a completely different meaning than *p++.

>
> Maybe an example of copying two strings;
>
> char *s1="test";
> char *s2="blab";


Make these const char *.

>
> for(unsigned int i=0;i<strlen(s1);i++)
> *s1++=*s2++;


This is wrong or otherwise ill-advised in several ways.

1) The technically correct type to use is size_t.

2) Recalculating the length on each iteration is unnecessarily inefficient.

3) Prefer pre-increment to post-increment.

4) Modifying a string literal (which is a const object) gives undefined
behavior.

Besides that, the general approach is definitely not correct in general.
You'd need to check the relative length of the source string against the
available length of the destination buffer, then loop based on the
source string. If you knew the destination length was adequate, you
could just do this:

while (*dest++ = *src++) { continue; }

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Benoit Mathieu
Guest
Posts: n/a
 
      04-17-2004

> while (*dest++ = *src++) { continue; }


Would you find this one less readable ?

while (*dest++ = *src++) ;

Benoit
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-17-2004
Benoit Mathieu wrote:

>
>> while (*dest++ = *src++) { continue; }

>
>
> Would you find this one less readable ?
>
> while (*dest++ = *src++) ;
>


I find it less clear. A semi-colon in a place where they aren't usually
seen can be easily missed, making the reader think that the 'while'
controls (or is supposed to control) some statement that follows. It can
also look like an error, as if the programmer added the semi-colon at
the end of the line out of habit.

An explicit 'continue' statement removes all doubt about the intent.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Frane Roje
Guest
Posts: n/a
 
      04-17-2004
"Kevin Goodsell" <(E-Mail Removed)> wrote in message
news:rifgc.14914$(E-Mail Removed) hlink.net...
> Frane Roje wrote:


> (*p)++ has a completely different meaning than *p++.


How?

> > Maybe an example of copying two strings;
> >
> > char *s1="test";
> > char *s2="blab";

>
> Make these const char *.

if they were const wouldn't that make then disabled for any kind of change?

> >
> > for(unsigned int i=0;i<strlen(s1);i++)
> > *s1++=*s2++;

>
> This is wrong or otherwise ill-advised in several ways.


What is advised?(exepct for strcpy(), or maybe strncpy()(I'm not sure if
this one
exists))

> 1) The technically correct type to use is size_t.

Isn't size_t actualy an unsigned integer?

> 3) Prefer pre-increment to post-increment.

Why?


> Besides that, the general approach is definitely not correct in general.
> You'd need to check the relative length of the source string against the
> available length of the destination buffer, then loop based on the
> source string. If you knew the destination length was adequate, you
> could just do this:


--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Simple question: Cannot convert 2D array to pointer-to-pointer overbored C++ 5 09-12-2004 10:11 AM



Advertisments