char array function problem

L

lothar.behrens

Hi,

I had or even have a problem with functions, that are returning char
arrays.
I want to translate text that should be internationalized. So the
following
function definition, I thought, would be good:

char* translateText(char* text);

This function could be used very simply at any place. But what is, if
the
translated text is bigger than the orginal string and the string is a
constant ?

Then I have defined a global char* pointer to be used. Every time I
translate
a text, I realloc needed memory and then translate.

The reallocation is done in an extern locale implementation, so not
shown
here.

Now the problem: I cannot use more than one call to translateText for a

function parameter. There would be an overwrite of the first use by the
second use of that function, before the real function gets the
translated
text.

How to solve this ?
Are there usual solutions ?

Thanks

Lothar

This is the current code:

char* translated = NULL;

char* translateText(char* text) {
Locale locale;
locale->translate(&translated, text);

return translated;
}

char a[] = "Car";
char b[] = "Tree";

printf("%s is %s and %s is %s\n", a, translateText(a), b,
translateText(b));
 
P

Peter Koch Larsen

Hi,

I had or even have a problem with functions, that are returning char
arrays.
I want to translate text that should be internationalized. So the
following
function definition, I thought, would be good:

char* translateText(char* text);

This function could be used very simply at any place. But what is, if
the
translated text is bigger than the orginal string and the string is a
constant ?

Then I have defined a global char* pointer to be used. Every time I
translate
a text, I realloc needed memory and then translate.

The reallocation is done in an extern locale implementation, so not
shown
here.

Now the problem: I cannot use more than one call to translateText for a

function parameter. There would be an overwrite of the first use by the
second use of that function, before the real function gets the
translated
text.

How to solve this ?
Are there usual solutions ?

Yes. Don't use raw pointers. Use a std::string or something else more
suitable to the solution. Also return the result in a copy of the original -
do not use in-copy replacement.

/Peter
Thanks

Lothar

This is the current code:

char* translated = NULL;

char* translateText(char* text) {
Locale locale;
locale->translate(&translated, text);

return translated;
}

char a[] = "Car";
char b[] = "Tree";

printf("%s is %s and %s is %s\n", a, translateText(a), b,
translateText(b));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top