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 support@ccsinfo.com

Return struct from a function.

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



Joined: 30 Jan 2018
Posts: 5

View user's profile Send private message

Return struct from a function.
PostPosted: Fri Mar 09, 2018 3:33 am     Reply with quote

Hi,

Using v5.073 version of the PICC compiler on a PIC16LF1827 here. And am facing a problem when trying to return a structure from a function call. According to this and this, I believe below code should work. However, I'm getting the following error: Error#27 Expression must evaluate to a constant.

Code (the error is on the last line):
Code:

struct tmr_246_config {
    int txcon;
    int prx;
};
typedef struct tmr_246_config tmr_246_config_t;

tmr_246_config_t mif_tmr6_get_conf(void)
{
    tmr_246_config_t tmr_config;
    tmr_config.txcon = REG_T6CON;
    tmr_config.prx = REG_PR6;
    return tmr_config;
}

//-----------------------------------

tmr_246_config_t tmr6_config = mif_tmr6_get_conf();


Returning a pointer to a struct works fine. So the following compiles OK:

Code:

struct tmr_246_config {
    int txcon;
    int prx;
};
typedef struct tmr_246_config tmr_246_config_t;

tmr_246_config_t * mif_tmr6_get_conf(void)
{
    tmr_246_config_t * tmr_config;
    tmr_config->txcon = REG_T6CON;
    tmr_config->prx = REG_PR6;
    return tmr_config;
}
//-----------------------------------
tmr_246_config_t *tmr6_config = mif_tmr6_get_conf();


Any ideas where my understanding is wrong? Why doesn't the first piece of code work? Am I completely misunderstanding the topic of returning structs and using typedefs?


Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 13748

View user's profile Send private message

PostPosted: Fri Mar 09, 2018 3:57 am     Reply with quote

Two separate issues:

Second first. In the pointer version, you never actually declare any memory to hold the variable. So your pointer is probably pointing off into the RAM somewhere and other things will get overwritten. You need to actually declare a variable and point the pointer to this, or use malloc to allocate some RAM and point to this.

Now the first. This is perfectly OK, except for one thing:
Code:

    tmr_246_config_t tmr6_config;
    tmr6_config = mif_tmr6_get_conf();


When you declare a variable and initialise it at declaration, the initialisation is done at compile time, and must be to a constant. Hence the error.

You have to declare the variable, then call the function.
xMerlin



Joined: 30 Jan 2018
Posts: 5

View user's profile Send private message

PostPosted: Fri Mar 09, 2018 4:43 am     Reply with quote

Thanks. First case is clear now. And rather obvious, I'm ashamed to admit.

As for the second, would this then work properly?
Code:

struct tmr_246_config {
    int txcon;
    int prx;
};
typedef struct tmr_246_config tmr_246_config_t;

tmr_246_config_t * mif_tmr6_get_conf(void)
{
    tmr_246_config_t tmr_config;
    tmr_config.txcon = REG_T6CON;
    tmr_config.prx = REG_PR6;
    return &tmr_config;
}
//-----------------------------------
tmr_246_config_t tmr6_config;
tmr_246_config_t *p_tmr6_config = &tmr6_config;
p_tmr6_config = mif_tmr6_get_conf();

It wouldn't, right? Because the memory assigned to the function's internal variable/structure would get freed after the function returns so contents would be unknown. Is my thinking correct?

As for the malloc, I get a Error#12 Undefined identifier -- MALLOC error when trying to use it. Is it even supported in PCW?
Ttelmah



Joined: 11 Mar 2010
Posts: 13748

View user's profile Send private message

PostPosted: Fri Mar 09, 2018 7:02 am     Reply with quote

Yes, malloc is supported. You have to include stdlibm.h

If you declare the variable as 'static' then the second would work properly. Otherwise as you say the RAM could be being re-used, but (of course), if you simply copy the value out of it as soon as you return, then it could potentially be used.
xMerlin



Joined: 30 Jan 2018
Posts: 5

View user's profile Send private message

PostPosted: Fri Mar 09, 2018 7:08 am     Reply with quote

Aha. And the required inclusion of stdlibm.h is stated so clearly in the help file. Embarassed Thank you.

Is there any obvious advantage of using pointers in this case vs just passing the entire structure and using struct?
Ttelmah



Joined: 11 Mar 2010
Posts: 13748

View user's profile Send private message

PostPosted: Fri Mar 09, 2018 9:26 am     Reply with quote

If the structure was a lot larger, the pointer would be much smaller to move. Otherwise, not really.
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