• AVR Freaks

Hot!Trouble assigning a pointer to a structure

Page: 12 > Showing page 1 of 2
Author
JustRob
Super Member
  • Total Posts : 426
  • Reward points : 0
  • Joined: 2008/09/04 12:49:27
  • Location: 0
  • Status: offline
2019/06/14 11:41:14 (permalink)
0

Trouble assigning a pointer to a structure

This is a really basic code question that I'm embarrassed I have to ask.
 
I'm using a pic16f1937 with the latest MPLAB X and the XC8 compiler and I'm using the ICD3 to step through my code.
 
I create this structure:
typedef struct {
    const bitmap_t * const bitmap;
    const UINT8 glyph_count;
    const glyph_t *glyphs; //pointer to array of glypt_t elements
} font_t;
 
 
In my code I assign a pointer to font_t with:
 
    extern const font_t FONT_arial_black_Qq_16;
    font_t const * temp_font = &FONT_arial_black_Qq_16;

 
When I step through the code the address of FONT_arial_black_Qq_16 = 0x881.  After I step past the pointer assignment the value = Invalid Address for all 3 data types in the font_t structure variable.  
 
The watch window says "temp_font" is type struct not struct*
 
Where am I going wrong?
 
Thanks!
 
post edited by JustRob - 2019/06/14 14:29:55
#1

38 Replies Related Threads

    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:12:47 (permalink)
    0
    I just realized that the font_t instance I use "temp_font" the watch window says it is type struct and not struct*.
     
    I don't understand why "temp_font" isn't seen as a pointer.
    #2
    du00000001
    Just Some Member
    • Total Posts : 2764
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:25:57 (permalink)
    -3 (3)
    The address of a struct is represented by its name.
    Try
    font_t *temp_font = FONT_arial_black_Qq_16;


    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:34:22 (permalink)
    0
    Yeah, I tried that but it doesn't compile with this error:
     
    mkr-7-main.c:103:19: error: initializing 'const font_t *' with an expression of incompatible type 'const font_t'; take the address with &
    font_t const *temp_font = FONT_arial_black_Qq_16;
    #4
    du00000001
    Just Some Member
    • Total Posts : 2764
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:38:40 (permalink)
    0
    Oh yeah - these "const"s can be unnerving...

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11208
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:46:32 (permalink)
    +1 (1)
    The address of a struct is represented by its name.

     
    It most certainly is not.  The correct code is:
        extern const font_t FONT_arial_black_Qq_16;
        font_t const * temp_font = &FONT_arial_black_Qq_16;

    #6
    1and0
    Access is Denied
    • Total Posts : 9287
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:47:26 (permalink)
    +2 (2)
    The & operator is required.  Try
    font_t const * temp_font = &FONT_arial_black_Qq_16;

     
    Edit: Jtemples beats me to it. ;)
     
    #7
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 12:58:47 (permalink)
    -1 (1)
    font_t const * temp_font = &FONT_arial_black_Qq_16;
        

     
    That's exactly what I have.
     
    What's interesting is "temp_font" is type struct in the watch window.
    #8
    1and0
    Access is Denied
    • Total Posts : 9287
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 13:07:10 (permalink)
    +2 (2)
    JustRob
    font_t const * temp_font = &FONT_arial_black_Qq_16;
        

    That's exactly what I have.

    No, you have this
    font_t * temp_font = &FONT_arial_black_Qq_16;

    without const.
     
    #9
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 13:10:57 (permalink)
    0
    I did in my original post but now I have what I posted two comments ago.  I copied it from my code.
    #10
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11208
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 13:52:48 (permalink)
    +1 (1)
    Does your code work?  It sounds like your issues are related to the debugger.
    #11
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 13:53:31 (permalink)
    0
    typedef struct {
        const bitmap_t * const bitmap;
        const UINT8 glyph_count;
        const glyph_t *glyphs; //pointer to array of glypt_t elements
    } font_t;

     
    Maybe it's how I define the font_t structure?
    #12
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:05:17 (permalink)
    0
    I attached a screenshot of the mouse-over of temp_font ~ I'm hoping that will help.

    Attached Image(s)

    #13
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11208
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:12:31 (permalink)
    0
    It looks like you're trying to view temp_font before it has been initialized.
    #14
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:18:31 (permalink)
    0
    No, I paused the debug run session while the code was in the endless while loop (and there's no code in the while loop).
    #15
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11208
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:30:13 (permalink)
    +1 (1)
    Is it actually being used?  You haven't shown enough code for us to know if it's been optimized away.
    #16
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:35:31 (permalink)
    0

        const char backrest_temp_string[] = "Backrest temp = ";
        extern const font_t FONT_arial_black_Qq_16;
        font_t const * temp_font = &FONT_arial_black_Qq_16;
        UINT16 pos_x = 10;
        UINT16 pos_y = 150;
        
        SPI_init();
        cf_display_init();

        LCD_BL_LATCH = 1;

        /****************************************************
         * *
         * NOTE: I'm using the crystalfontz 3.5" LCD with a *
         * resolution of 320 x 240 in a portrait *
         * configuration where the x = 0 to x = 320 *
         * which is normally along the top of the *
         * display in landscape mode is now the right *
         * side of the display. *
         * *
         ****************************************************/
        
        Fill_LCD(0xFF, 0xFF, 0xFF);
        Fill_mkr_screen();

        // gfx_write_text(const font_t temp_font, UINT16 pos_x, UINT16 pos_y, const char *backrest_temp_string)
        gfx_write_text(temp_font, pos_x, pos_y, backrest_temp_string);


        while(1){
            

        }
    }

     
    It is used just before the while loop:
        gfx_write_text(temp_font, pos_x, pos_y, backrest_temp_string);

     
    When I step into gfx_write_text tepm_font points to Invalid Address.
    post edited by JustRob - 2019/06/14 14:38:55
    #17
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11208
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 14:38:34 (permalink)
    +1 (1)
    Since temp_font is unused after the call to gfx_write_text, the compiler can optimize it away at the point you're looking at it.  Making it static and/or volatile might make it visible in the debugger.
    #18
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 15:03:58 (permalink)
    0
    I set the code to:
        static font_t const * temp_font = &FONT_arial_black_Qq_16;

    and:
        volatile static font_t const * temp_font = &FONT_arial_black_Qq_16;

     
    Both times before I step into:
        gfx_write_text(temp_font, pos_x, pos_y, backrest_temp_string);

     
    When I mouse-over temp_font (and in the watch window) it says [ temp_font ] Not recognized
     
    Then when I step into gtx_write_text and look at temp_font that is passed in the address is 0x8881 and the value is Invalid Address for the 3 members of the font_t struct (the address of FONT_arial_black_Qq_16 = 0x881 ~ coincidence?).
     
    Should not the watch window show temp_font as type font_t*?
     
    Thanks!
    #19
    JustRob
    Super Member
    • Total Posts : 426
    • Reward points : 0
    • Joined: 2008/09/04 12:49:27
    • Location: 0
    • Status: offline
    Re: Trouble assigning a pointer to a structure 2019/06/14 15:28:46 (permalink)
    0
    @jtemples
    Does your code work? It sounds like your issues are related to the debugger.

     
    No, I'm not getting the font glyph.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5