Jump to content
Sign in to follow this  
mp035

*ptr++ And Ptr++ Both Increment Ptr

Recommended Posts

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.

Share this post


Link to post
Share on other sites

No error here. *ptr++ increments pointer, (*ptr)++ increments pointer content.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...