CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pointer confused

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Pointer confused
PostPosted: Tue Mar 04, 2014 1:20 pm     Reply with quote

Small example, for pointer to string confusion?

What I want to ask is the generated asm code, it it real different?

Compiler:5020.

EX1 Code:
Code:
#include <18F26K22.h>

/*Only test program, not for production in PIC*/

char *ptr;//Pointer to a pre. defined string -> String = 0123456789;

void Test(){
 char tmp;
 tmp= *ptr;    //is this '0'
 tmp=  ptr[0]; //is this '0'
 
 tmp= *(ptr+1);//is this '1'
 tmp=  ptr[1]; //is this '1'
}

void main(){
 Test();
}


EX1 Out:
Code:

.................... void Test(){
....................  char tmp;
....................  tmp= *ptr;    //is this '0'
00004:  MOVFF  ptr,FSR0L
00008:  MOVFF  ptr+1,FSR0H
0000C:  MOVFF  INDF0,tmp
....................  tmp=  ptr[0]; //is this '0'
00010:  MOVFF  ptr,FSR0L
00014:  MOVFF  ptr+1,FSR0H
00018:  MOVFF  INDF0,tmp
....................   
....................  tmp= *(ptr+1);//is this '1'
0001C:  MOVLW  01
0001E:  ADDWF  ptr,W
00020:  MOVWF  @01
00022:  MOVLW  00
00024:  ADDWFC ptr+1,W
00026:  MOVFF  01,FSR0L
0002A:  MOVWF  FSR0H
0002C:  MOVFF  INDF0,tmp
....................  tmp=  ptr[1]; //is this '1' 
00030:  MOVLW  01
00032:  ADDWF  ptr,W
00034:  MOVWF  FSR0L
00036:  MOVLW  00
00038:  ADDWFC ptr+1,W
0003A:  MOVWF  FSR0H
0003C:  MOVFF  INDF0,tmp
00040:  GOTO   0062 (RETURN)
.................... }


***
EX2 Code:
Code:
#include <18F26K22.h>

/*Only test program, not for production in PIC*/

char *ptr[2];//Pointer array to a pre. defined string -> String = 0123456789;

void Test(){
 char tmp;
 tmp= *(ptr[0]);      //is this '0'
 tmp=  ptr[0][0];     //is this '0'
 
 tmp= *(ptr[0]+1);  //is this '1'
 tmp=  ptr[0][1];     //is this '1'
}

void main(){
 Test();
}


Ex2 Out:
Code:

.................... void Test(){
....................  char tmp;
....................  tmp= *(ptr[0]);    //is this '0'
00004:  MOVFF  ptr,FSR0L
00008:  MOVFF  ptr+1,FSR0H
0000C:  MOVFF  INDF0,tmp
....................  tmp=  ptr[0][0]; //is this '0'
00010:  MOVFF  ptr,tmp
....................   
....................  tmp= *(ptr[0]+1);//is this '1'
00014:  MOVLW  01
00016:  ADDWF  ptr,W
00018:  MOVWF  @01
0001A:  MOVLW  00
0001C:  ADDWFC ptr+1,W
0001E:  MOVFF  01,FSR0L
00022:  MOVWF  FSR0H
00024:  MOVFF  INDF0,tmp
....................  tmp=  ptr[0][1]; //is this '1' 
00028:  MOVFF  ptr+1,tmp
0002C:  GOTO   004E (RETURN)
.................... }
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Wed Mar 05, 2014 1:52 am     Reply with quote

A search here will find quite a bit about this.

Report it to CCS.

Problems with multi-dimensional pointer arrays have been common. They tend to come and go with versions.

In the past I have suggested deliberate casting as the most reliable way of avoiding problems. So explicitly storing the pointers as int16, and then casting them to 'char *' on retrieval.
It had been fixed a few versions ago.

As you have found, there is only a problem when they are used/stored as *ptr[], not as ptr[][].

Just 'FYI', realise that as posted, the compiler _pre solves_ the location of ptr[0][0] (for example), to access the data stored there, rather than using the indirect access when the pointer is used.

Worth posting your test example, so we can see the pointers are correctly loaded:
Code:

void main(){
 char data[]="0123456789";
 ptr[0]=data;
 ptr[1]=data;
 Test2();
 while(TRUE);
}

For example.



Best Wishes
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 6:56 am     Reply with quote

Hi

Just to inform that CCS have fixed the issue. It will be in next release:-)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group