Issue assigning the value of a pointer to a char variable in XC8

Author
mario.ortiz
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2017/10/15 09:56:36
  • Location: 0
  • Status: offline
2017/12/04 17:13:53 (permalink)
0

Issue assigning the value of a pointer to a char variable in XC8

Hi everybody,
 
I'm trying to assign the value of a pointer to a char variable in XC8. The char's value didn't change, it is still 0.
 
Here is the code:
 
void function(unsigned char * datas,unsigned int num)
{
unsigned int i;
unsigned char tempOriginal;
unsigned int tempcol=0;
unsigned int templine=0;
 
for(i=0;i<num;i++)
{
tempOriginal=*(datas+templine);
templine++;
}
 
Thanks a lot.
 
Best regards,
#1

18 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/04 18:09:18 (permalink)
    +2 (2)
    How did you test this?
    The bug is most probably in the code you didn't show.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    NorthGuy
    Super Member
    • Total Posts : 5101
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/04 18:55:03 (permalink)
    +1 (1)
    Most likely tempOriginal has been eliminated by the compiler because it is not used anywhere.
    #3
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/04 19:22:13 (permalink)
    0
    Hi everybody, I test this with a breakpoint in the line which is written "templine++". The code I didn't show only send by SPI the variable tempOriginal.
     
    Thanks a lot.
     
    Best regards,
    #4
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/04 20:23:58 (permalink)
    +1 (1)
    As NorthGuy said, the code you posted never uses tempOriginal, so if it is not marked as "volatile", then the compiler is smart enough to throw away any code writing to it, because it is never used.
    That means code you think "does not matter", really "does matter" !
     
    If something odd is happening, post your REAL code, not a summary of what you think your code is doing.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 16659
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/04 20:43:26 (permalink)
    +2 (2)
    In short the code appears to be ok.
    This means either:
    The issue is else where.
    Or you test code is digging the optimizer.

    Or there could be other issues.
    What version of MPLabX and compiler are you using?
    #6
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 04:29:56 (permalink)
    0
    Hi, thanks for the replies. Here is the complete code:
     
    void function(unsigned char * datas,unsigned int num)
    {
    unsigned int i; 
    unsigned char tempOriginal; 
    unsigned int templine=0;
     
    for(i=0;i<num;i++)

    tempOriginal=*(datas+templine);
    templine++;
    }
    SendSPI(tempOriginal);
     
    }
     
    The compiler I use is XC8 and the the MPLAB version is 3.61.
     
    Thanks a lot!
     
    Best regards,
    #7
    andersm
    Super Member
    • Total Posts : 2485
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: online
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 06:00:12 (permalink)
    +1 (1)
    Is the SendSPI() call supposed to be inside the loop? That code can be reduced to
    void function(unsigned char *datas, unsigned int num)
    {
      SendSPI(datas[num-1]);
    }

    #8
    rodims
    Super Member
    • Total Posts : 1377
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 06:02:55 (permalink)
    +1 (1)
    marioI'm trying to assign the value of a pointer to a char variable in XC8. The char's value didn't change, it is still 0

     
    How do you know it did not change ?  How do you test ? If your data source contains 0's or the pointer arithmetic works different from what you think it should do, then the shown code might not contain any problem.
    Initialize tempOriginal to 0x42 and see what happens.
    Your code shown still is not "complete" in the sense that we could see the problem. This also depends on your expectations. If you post more code, please use code tags.
     
    #9
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 11:31:59 (permalink)
    0
    I tested, debugging the code, when I initialize tempOriginal to 0x42, it works.
    #10
    rodims
    Super Member
    • Total Posts : 1377
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 12:38:06 (permalink)
    0
    Hm, that's the power of the number 42 wink: wink
    I assumed that "not initializing" the stack variable tempOriginal by chance (or due to debugging) could have a value of 0.  Using 0x42 should only help you to see during debugging, that the value changes (is overwritten) during assignment in the loop. I don't think the initialization changes anything in your program logic, so it probably also "worked" before. 
     
    #11
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 15:51:49 (permalink)
    0
    Hi in the link below you can see the value of the variable tempOriginal.
     
    https://ibb.co/jvCJ2w
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2768
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/05 16:43:37 (permalink)
    +1 (1)
    42 is the answer to the Universe.
     
    datas is already a pointer, you don't need the cast: ptr to ptr.
     
    Here's a rough idea.   (I did delete some of the lines due to copy and paste)
    void function(unsigned char * Datas, unsigned int Num){
    unsigned int templine=0;
    char *ptr;
     
    for(i=0;i<Num;i++){
        //tempOriginal=*(Datas+templine);
        ptr=Datas+templine;  //split into two lines
        tempOriginal=*ptr;
        templine++;
    }


    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/06 12:06:57 (permalink)
    0
    it didn't work, the result is the same.
    #14
    NorthGuy
    Super Member
    • Total Posts : 5101
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/06 12:29:40 (permalink)
    +1 (1)
    If you think the compiler doesn't do things right, look at the disassembly.
    #15
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2768
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/06 16:46:49 (permalink)
    0
    That is illogical Jim.
     
    "void function(unsigned char * datas,unsigned int num)"
    You missed out, "begin". (unfunny Pascal joke)
     
    What data is func. receiving?  Did you pass the pointer correctly?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #16
    mario.ortiz
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2017/10/15 09:56:36
    • Location: 0
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/07 16:09:31 (permalink)
    0
    Hi everybody, I found the issue. It was an errata of the K40 family. It doesn't understand when you define a const variable like this: const var[]={0x00,0x00}.
    To resolve the issue I needed to add to the project an assambly code named power-up.as.
    The code must have this lines:
    #include <xc.inc>
    GLOBAL powerup, start
    PSECT powerup, class="CODE", delta=1, reloc=2
    powerup:
    BSF NVMCON1, 7
    GOTO start
    end

     
    Thanks everybody!
     
    Best regards,
    post edited by mario.ortiz - 2017/12/07 16:10:46
    #17
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/07 16:26:29 (permalink)
    +2 (2)
    The compiler now has the capability to do that for you. Read the readme file that comes with it.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #18
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Issue assigning the value of a pointer to a char variable in XC8 2017/12/07 16:55:37 (permalink)
    +1 (1)
    Hence why we usually ask posters to specify WHICH PIC they are using.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #19
    Jump to:
    © 2018 APG vNext Commercial Version 4.5