For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > How to increment both the pointer and the content of the pointer









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to increment both the pointer and the content of the pointer
Ben

2006-05-21, 7:11 pm

How do you increment a pointer to an integer
(increment both the pointer and the content of the pointer)?


Mark Randall

2006-05-21, 7:11 pm

int* blah = somearray;
blah++;

?

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."
"Ben" <boningho@gmail.com> wrote in message
news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl...
> How do you increment a pointer to an integer
> (increment both the pointer and the content of the pointer)?
>



John Carson

2006-05-21, 7:11 pm

"Ben" <boningho@gmail.com> wrote in message
news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl
> How do you increment a pointer to an integer
> (increment both the pointer and the content of the pointer)?


How about :

#include <iostream>
using namespace std;

int main()
{
int array[] = {0,1,2,3,4,5,6,7,9};
size_t arraySize = sizeof(array)/sizeof(array[0]);
int *ptr = array;

// display initial array
for(size_t i=0; i<arraySize; ++i)
cout << array[i] << '\n';

cout << "\n\n";

// do the incrementing
while (ptr != array+arraySize )
{
++*ptr;
++ptr;
}

// display modified array
for(size_t i=0; i<arraySize; ++i)
cout << array[i] << '\n';


return 0;
}


--
John Carson



Jonathan Wood

2006-05-21, 10:09 pm

int *pInt;

// Initialize pInt to point to an integer

// Increment the content of the pointer
*pInt;

// Increment the pointer (will now point to something else)
pInt++;

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Ben" <boningho@gmail.com> wrote in message
news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl...
> How do you increment a pointer to an integer
> (increment both the pointer and the content of the pointer)?
>



Ben

2006-05-21, 10:09 pm

Dear John,

This is GREAT, thanks you a lot.

Ben

"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:eCqeJvTfGHA.1276@TK2MSFTNGP03.phx.gbl...
> "Ben" <boningho@gmail.com> wrote in message
> news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl
>
> How about :
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int array[] = {0,1,2,3,4,5,6,7,9};
> size_t arraySize = sizeof(array)/sizeof(array[0]);
> int *ptr = array;
>
> // display initial array
> for(size_t i=0; i<arraySize; ++i)
> cout << array[i] << '\n';
>
> cout << "\n\n";
>
> // do the incrementing
> while (ptr != array+arraySize )
> {
> ++*ptr;
> ++ptr;
> }
>
> // display modified array
> for(size_t i=0; i<arraySize; ++i)
> cout << array[i] << '\n';
>
>
> return 0;
> }
>
>
> --
> John Carson
>
>
>



Ben

2006-05-21, 10:09 pm

another question:

can I combine these two line:
++*ptr;
++ptr;
into a single statement?



"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:eCqeJvTfGHA.1276@TK2MSFTNGP03.phx.gbl...
> "Ben" <boningho@gmail.com> wrote in message
> news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl
>
> How about :
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int array[] = {0,1,2,3,4,5,6,7,9};
> size_t arraySize = sizeof(array)/sizeof(array[0]);
> int *ptr = array;
>
> // display initial array
> for(size_t i=0; i<arraySize; ++i)
> cout << array[i] << '\n';
>
> cout << "\n\n";
>
> // do the incrementing
> while (ptr != array+arraySize )
> {
> ++*ptr;
> ++ptr;
> }
>
> // display modified array
> for(size_t i=0; i<arraySize; ++i)
> cout << array[i] << '\n';
>
>
> return 0;
> }
>
>
> --
> John Carson
>
>
>



Carl Daniel [VC++ MVP]

2006-05-21, 10:09 pm

"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:eCqeJvTfGHA.1276@TK2MSFTNGP03.phx.gbl...
> "Ben" <boningho@gmail.com> wrote in message
> news:ey0RUhTfGHA.1264@TK2MSFTNGP05.phx.gbl
>


> // do the incrementing
> while (ptr != array+arraySize )
> {
> ++*ptr;
> ++ptr;


or, for the really istic, replace the two lines above with:

++*ptr++;

-cd


Ben

2006-05-21, 10:09 pm

Is this two statements equal?

++*ptr++;
(++(*(ptr++)));



"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:OAo5jBUfGHA.764@TK2MSFTNGP03.phx.gbl...
> "John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
> news:eCqeJvTfGHA.1276@TK2MSFTNGP03.phx.gbl...
>
>
> or, for the really istic, replace the two lines above with:
>
> ++*ptr++;
>
> -cd
>
>



Igor Tandetnik

2006-05-21, 10:09 pm

"Ben" <boningho@gmail.com> wrote in message
news:OlPFI$TfGHA.5088@TK2MSFTNGP02.phx.gbl
> another question:
>
> can I combine these two line:
> ++*ptr;
> ++ptr;
> into a single statement?


++(*ptr++);

but just because you can doesn't mean it's actually a good idea to use
something cute like this in your production code.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Carl Daniel [VC++ MVP]

2006-05-22, 4:19 am

Ben wrote:
> Is this two statements equal?
>
> ++*ptr++;
> (++(*(ptr++)));


Yes. From the inside out:

1. take the value of ptr
2. increment ptr
3. dereference the value obtained in step 1
4. increment the value at that address

-cd


Ben

2006-05-22, 4:19 am

Thank you, now I make more clear on pointer behavior.

Ben



"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:O7uUe4VfGHA.4304@TK2MSFTNGP05.phx.gbl...
> Ben wrote:
>
> Yes. From the inside out:
>
> 1. take the value of ptr
> 2. increment ptr
> 3. dereference the value obtained in step 1
> 4. increment the value at that address
>
> -cd
>
>



David Webber

2006-05-22, 7:15 pm


"Ben" <boningho@gmail.com> wrote in message
news:Oyp0mWWfGHA.1792@TK2MSFTNGP03.phx.gbl...

> Thank you, now I make more clear on pointer behavior.


I'd advise getting a table of C++ operator precedence rules if you're even
*thinking* of doing anything like this!

And irrespective of how confident you are, still use brackets or multiple
statements.

Even in simple case like

y = a*x+b;

I often prefer

y = (a*x) + b;

though in that case I usually don't go quite as far as

y = a*x;
y += b;

The brackets, when they are not needed, cost nothing in your code. Missing
them out where they are needed can cost hours of debugging time, or worse.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


Carl Daniel [VC++ MVP]

2006-05-22, 7:15 pm

"David Webber" <dave@musical.demon.co.uk> wrote in message
news:%23%23$nQ4bfGHA.4496@TK2MSFTNGP03.phx.gbl...
>
> "Ben" <boningho@gmail.com> wrote in message
> news:Oyp0mWWfGHA.1792@TK2MSFTNGP03.phx.gbl...
>
>
> I'd advise getting a table of C++ operator precedence rules if you're even
> *thinking* of doing anything like this!


Absolutely! While I did offer the example, if I saw code like that during a
code review, I'd be having a talk with the author!

>
> And irrespective of how confident you are, still use brackets or multiple
> statements.
>
> Even in simple case like
>
> y = a*x+b;
>
> I often prefer
>
> y = (a*x) + b;
>
> though in that case I usually don't go quite as far as
>
> y = a*x;
> y += b;
>
> The brackets, when they are not needed, cost nothing in your code.
> Missing them out where they are needed can cost hours of debugging time,
> or worse.


Agreed 100%!

-cd


Tim Roberts

2006-05-24, 4:16 am

"Jonathan Wood" <jwood@softcircuits.com> wrote:

>int *pInt;
>
>// Initialize pInt to point to an integer
>
>// Increment the content of the pointer
>*pInt;


Why do you think that increments the content of the pointer?
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Jonathan Wood

2006-05-24, 7:10 pm

Because I did not realize I had a syntax error when I wrote it.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Tim Roberts" <timr@probo.com> wrote in message
news:ots7721mfgt9o6fc64bieviqk5qfg8k2ns@
4ax.com...
> "Jonathan Wood" <jwood@softcircuits.com> wrote:
>
>
> Why do you think that increments the content of the pointer?
> --
> - Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com