473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

returning a const char*

Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.

I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
....
and so on.

Now I want to create some macros that I could use all over the place in all
the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one
....

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );

but my problem is the return value, I would like to return a const char* or
even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?

I need to return the function otherwise the macro are not very useful really
because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?

Many thanks.

Simon
Jul 22 '05 #1
8 4551

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2h************@uni-berlin.de...
Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.
Macros are part of the standard.

I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
...
and so on.

Now I want to create some macros that I could use all over the place in all the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one ...

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );
No reason for macros, inline functions would be better.

but my problem is the return value, I would like to return a const char* or even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?
Not really, its obscure and therefore dangerous.

I need to return the function otherwise the macro are not very useful really because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?


Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());

john
Jul 22 '05 #2
Thanks for your reply,

Macros are part of the standard.

ah, thanks.
Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());
But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()


john


Simon.
Jul 22 '05 #3

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2h************@uni-berlin.de...
Thanks for your reply,

Macros are part of the standard.


ah, thanks.
Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());


But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()


Only if GetLanguageFile returns a std::string. My inline function would work
whether GetLanguageFile returns char*, const char* or std::string.

But why use a macro? They have no advantage in this case.

john
Jul 22 '05 #4

But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()

Only if GetLanguageFile returns a std::string. My inline function would

work whether GetLanguageFile returns char*, const char* or std::string.
I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.

But why use a macro? They have no advantage in this case.
Just curious really. If I could cut out the '.c_str()' it would be a bonus.

john


Simon
Jul 22 '05 #5

"Sims" <si*********@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()
Only if GetLanguageFile returns a std::string. My inline function would

work
whether GetLanguageFile returns char*, const char* or std::string.


I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.


A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a
char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it
then you should make it return a std::string.

But why use a macro? They have no advantage in this case.


Just curious really. If I could cut out the '.c_str()' it would be a

bonus.


Fair enough.

john
Jul 22 '05 #6
> >

A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it then you should make it return a std::string.
Yes it is my function. So i guess i will make it return std:string

But why use a macro? They have no advantage in this case.


Just curious really. If I could cut out the '.c_str()' it would be a

bonus.


Fair enough.

john

Thanks for your help.

Simon
Jul 22 '05 #7
My Suggestion:

struct Language
{
char* pOpen;
char* pClose;
char* pRead;
char* pWrite;
};
const Language English = {

"Open",
"Close",
"Read",
"Write" };

const Language Irish = {

"Oscail",
"Dún",
"Léigh",
"Scríobh" };


const Language* pCurrentLanguage = &English; //Default to English

void SetLanguage(const Language* const NewLanguage)
{
pCurrentLanguage = NewLanguage;
}

int main(void)
{
SetLanguage(&English);

cout << pCurrentLanguage.pOpen;

SetLanguage(&Irish);

cout << pCurrentLanguage.pOpen;
}

You may consider setting "SetLanguage" to take an enum instead of a pointer,
whatever tickles your fancy. I myself like this way!
Hope that helps.
-JKop
Jul 22 '05 #8
And once again I have missed the point!!
You can have language file like this:
char* pLoadedLanguage = "Open\0Close\0Read\0Write";
Language MakeLanguageStruct(const char* pLoadedLanguage)
{
Language NewLanguage;

NewLanguage.pOpen = pLoadedLanguage;
while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pClose = pLoadedLanguage;

while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pRead = pLoadedLanguage;

while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

Language.pWrite = pLoadedLanguage;
return NewLanguage;
}

Or something to that effect!!

-JKop
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
2
by: Lady_A | last post by:
I have created a basic COM in-proc server and a client. The registration of my server succeeds according to regsvr32. I can see it in the registry, having the ProgID and the InProcServer32...
9
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
19
by: Robert Smith | last post by:
I am wondering why it is possible to return a pointer to a string literal (ie. 1) but not an array that has been explicitly allocated. (ie. 2) ? Both would be allocated on the stack, why does the...
6
by: Siam | last post by:
Hi all... What's the best way to return some string from a function (without using the string class). Would the following work: char* getString( ) { return "TheString"; }
4
by: werasm | last post by:
Hi all, I have recently come accross this function declaration syntax. Initially it was puzzling (still is, to be honest), but I now realize that it declares a function that returns a reference...
36
by: MC felon | last post by:
how do we return strings or arrays from a function? i tried.. char some_func() //where i thought char would tell the compiler that the return is a string { char str; //something; return...
23
by: =?iso-8859-1?q?Santiago_Urue=F1a?= | last post by:
Hi, I tried to return a pointer to a constant string, but the compiler gives the following warning if a cast is not used: warning: assignment from incompatible pointer type This is the code:
1
by: krishna81m | last post by:
In the following code, I am trying to return a char, a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.