mp035 0 Posted March 21, 2006 Report Share Posted March 21, 2006 When using *ptr++ in a function, the pointer is incremented and not the contents. I think this is a bug in the compiler but am not an experienced c programmer so I could just be stupid. Source and disassembly below. Boostc for PIC16 v6.30 -- ------------------------------------------------------------------- #include "system.h" void ptr_test(unsigned char *ptr){ *ptr++; // this increments the pointer -- WRONG!! (I think) ptr++; // this also increments the pointer -- RIGHT *ptr+=1; // this increments the contents pointed to -- RIGHT ptr+=1; // this increments the pointer -- RIGHT } void main(){ unsigned char ptr_contents; ptr_test(&ptr_contents); } -- ------------------------------------------------------------------------- DISSASEMBLY - SHOWS THE FSR BEING INCREMENTED AND NOT INDF -- ------------------------------------------------------------------------- --- Filename removed by hand ------------------- 1: #include "system.h" 2: 3: void ptr_test(unsigned char *ptr){ 4: 5: *ptr++; // this increments the pointer - WRONG!! (I think) 000003 0AA1 INCF 0x21, F 000004 1903 BTFSC 0x3, 0x2 000005 0AA2 INCF 0x22, F 6: 7: ptr++; // this also increments the pointer - RIGHT 000006 0AA1 INCF 0x21, F 000007 1903 BTFSC 0x3, 0x2 000008 0AA2 INCF 0x22, F 8: 9: *ptr+=1; // this increments the contents pointed to - RIGHT 000009 1383 BCF 0x3, 0x7 00000A 1822 BTFSC 0x22, 0 00000B 1783 BSF 0x3, 0x7 00000C 0821 MOVF 0x21, W 00000D 0084 MOVWF 0x4 00000E 0A80 INCF 0, F 10: 11: ptr+=1; // this increments the pointer - RIGHT 00000F 3001 MOVLW 0x1 000010 07A1 ADDWF 0x21, F 000011 1803 BTFSC 0x3, 0 000012 0AA2 INCF 0x22, F 12: 13: } 000013 0008 RETURN 14: 15: void main(){ 16: unsigned char ptr_contents; 17: ptr_test(&ptr_contents); 000014 3000 MOVLW 0 000015 1283 BCF 0x3, 0x5 000016 1303 BCF 0x3, 0x6 000017 00A2 MOVWF 0x22 000018 3020 MOVLW 0x20 000019 00A1 MOVWF 0x21 00001A 2003 CALL 0x3 18: } 00001B 0008 RETURN -- ---------------------------------------------------------------------------- If my understanding of pointers is not up to scratch can someone tell me, but I figure that *ptr+=1; should be the same as *ptr++; which should be different to ptr++; Cheers. Quote Link to post Share on other sites
Pavel 0 Posted March 21, 2006 Report Share Posted March 21, 2006 No error here. *ptr++ increments pointer, (*ptr)++ increments pointer content. Regards, Pavel Quote Link to post Share on other sites
mp035 0 Posted April 21, 2006 Author Report Share Posted April 21, 2006 No error here. *ptr++ increments pointer, (*ptr)++ increments pointer content. Regards, Pavel <{POST_SNAPBACK}> Thanks, just shows my inexperience with c. Mark. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.