*Pointer++ cpp interpretation

Post Reply
FredCailloux
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am
Location: Quebec - Canada

*Pointer++ cpp interpretation

Post by FredCailloux » Tue Feb 09, 2016 6:55 pm

In the example pmodJSTK.cpp is defined an array recv[5] of type unsigned integer 8 bits as such: uint8_t recv[5];
Can you please confirm the following assumption:

Code: Select all

 uint8_t * myPointer ;
 myPointer = &recv[0] ;
 myPointer = myPointer + 1 ;
 *myPointer = spiCon.tranfer(ledSt) ; 
Above is equivalent to below ( from the pmodJSTK example cpp code)

Code: Select all

 *recv++ = spiCon.transfer(ledSt);		
I am not sure exactly how the compiler will interpret the *recv++ ?
Here is my interpretation of this code:
*recv is pointing to the first item, array recv[0].
*recv++ is pointing to second item, array recv[1]
Hence, recv[1] will endup taking the result of the function call spiCon.transfer(ledSt)
Is that what happen ?
Appreciate your input.
Prashant Tripathi :idea: Knowing is to the Self what knowledge is to the ego

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: *Pointer++ cpp interpretation

Post by majenko » Tue Feb 09, 2016 7:54 pm

This might be of some help to you:

http://en.cppreference.com/w/cpp/langua ... precedence

The recv++ is of a higher precedence than the *recv, so the recv++ go together as a unit and the * is then secondary. It's exactly the same as if you had the brackets:

Code: Select all

*(recv++)
And since the ++ is after the recv it's a post-increment, so it gets incremented after the assignment happens. So in long-hand it's:

Code: Select all

*recv = spiCon.transfer(ledSt);
recv++;
But you don't want to do that to a defined array because it would cripple the array. You should only ever do it to a pointer to an array, so:

Code: Select all

uint8_t *myPointer = recv;
*myPointer++ = spiCon.transfer(ledSt);
Note that recv and &recv[0] are the same - they are both pointers to the start of the array.

If you were to increment recv then recv[0] would actually be pointing to recv[1], which would be very bad.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

FredCailloux
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am
Location: Quebec - Canada

Re: *Pointer++ cpp interpretation

Post by FredCailloux » Tue Feb 09, 2016 10:51 pm

Thanks for the cppreference.com - it appears to be very well presented. I sure will bookmark this site.
You sure did clarified a few concepts here but you also got me confused. There is something here that appear to me as contradictory.
Can you please clarify. First you mention that

Code: Select all

*recv++  // is equivalent to 
*(recv++)
Fine, I can see that from the precedence table. Then you indicate that the ++ is post-increment, hence, the line

Code: Select all

*recv++ = spiCon.transfer(ledSt);   //  would be equivalent to 
*recv = spiCon.transfer(ledSt);     
recv++;
So, my confusion is if recv++ as precedence doesn't it mean that it will be incremented Before the dereference* operator ?
If so, the final result would be that recv[1] would take the function value, not recv[0]
What am I missing ?
Prashant Tripathi :idea: Knowing is to the Self what knowledge is to the ego

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: *Pointer++ cpp interpretation

Post by majenko » Wed Feb 10, 2016 12:13 am

What you're missing is that the "post" doesn't apply to the single operator, but to the whole of the assignment.

If it were:

Code: Select all

*++foo = blahblah;
then foo would be incremented before the assignment, not after.

For instance, this little bit of code (for a PC not a chipKIT):

Code: Select all

#include <stdio.h>

int foo[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main() {
    int i;
    int *foo2 = foo;
    *foo2++ = 23;

    for (i = 0; i < 10; i++) {
        printf("%d: %d\n", i, foo[i]);
    }
}
results in:

Code: Select all

0: 23
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9
As you can see the assignment happened before the increment. Similarly this bit of code:

Code: Select all

#include <stdio.h>

int main() {
    int foo = 0;
    int bar = foo++;
    printf("foo = %d bar = %d\n", foo, bar);
    return 0;
}
results in:

Code: Select all

foo = 1 bar = 0
because the increment of foo happens post the assignment of its value to bar.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

FredCailloux
Posts: 69
Joined: Sat Mar 28, 2015 4:45 am
Location: Quebec - Canada

Re: *Pointer++ cpp interpretation

Post by FredCailloux » Wed Feb 10, 2016 5:13 pm

I got it! I must admit, pointer stuff is not very "Reader Friendly" but hey! who am I to comment 33 years of C++ refinements.
Thanks a lot Majenko. By the way, is there something you don't know about C++ programming ? :) Joke :)
Prashant Tripathi :idea: Knowing is to the Self what knowledge is to the ego

User avatar
majenko
Site Admin
Posts: 2165
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: *Pointer++ cpp interpretation

Post by majenko » Wed Feb 10, 2016 6:10 pm

I don't know, but I do know there are things that I don't know about C++ programming ;)

Have you ever looked at C++ templates? They're "fun"! ;)
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply