dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Beginner
Apr 14, 2000

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Returning Data Structures from Functions

Question:
I'm working with a CCS compiler for the PIC16F877, so the rules may be different, but I've created my own data structure (named MERIT_long) to support >16bit values (CCS's largest data-type is a 16bit long); it has an 8bit char(MSB) and a 16bit long(LSB), as well as a 1bit short (rollover flag). I need to return this struct from a function, but the compiler complains.

Do I need to use pointers/references for this? If so, how? I assume this is part of regular C/C++ functionality, and possibly a limitation of the CCS compiler.

Answer:
It's a compiler-specific limitation. Both ANSI C and C++ allow you to return a struct by value from a function. However, it seems that your compiler uses only a subset of C/C++. This is common practice in embedded systems and controllers. As a workaround, you can return non-primitive data types by reference or by address:

MERIT_long * func() // return pointer
{
 MERIT_long * p = new MERIT_long;
 //... assign values 
 return p;
}

MERIT_long & func() // return reference
{
 static MERIT_long ml;
 //... assign values 
 return ml;
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date