473,796 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4580

"Simon" <sp********@sch oolsofafrica.co m> 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(LAN G_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(LAN G_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********@sch oolsofafrica.co m> 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(LAN G_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*********@ho tmail.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* pCurrentLanguag e = &English; //Default to English

void SetLanguage(con st Language* const NewLanguage)
{
pCurrentLanguag e = NewLanguage;
}

int main(void)
{
SetLanguage(&En glish);

cout << pCurrentLanguag e.pOpen;

SetLanguage(&Ir ish);

cout << pCurrentLanguag e.pOpen;
}

You may consider setting "SetLanguag e" 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\0R ead\0Write";
Language MakeLanguageStr uct(const char* pLoadedLanguage )
{
Language NewLanguage;

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

pLoadLanguage +=1;

NewLanguage.pCl ose = pLoadedLanguage ;

while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pRe ad = 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
2146
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 problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
4
1824
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. Consider, void f(int x, int& i); int i, j; f(1, i);
2
9535
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 entries. I can't seem to find it in the OLE/COM Viewer unless I specifically open the tlb file. Opening the dll file by the same method displays an error loading the type lib, although the dialog box specifies that I can put in a dll file.
9
10168
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
19
2502
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 first one not cause a compiler warning? #include <stdio.h> char * funca() { char *a = "blah"; //1 - ok // char a = "blah"; //2 - not ok
6
10974
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
2211
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 to an array. The declarator: char (& foo() ); //(1)
36
2289
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 str; }
23
4785
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
1725
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 all is how variables are created in what type of memory and how they are deleted in the following three cases when calling and exiting test, test1 and test2 functions. I also note that I see a different result while using char and char* which I...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.