473,395 Members | 1,568 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,395 software developers and data experts.

char array function problem

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));

Jul 23 '05 #1
1 1228

<lo************@lollisoft.de> skrev i en meddelelse
news:11*********************@o13g2000cwo.googlegro ups.com...
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));

Jul 23 '05 #2

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

Similar topics

10
by: techie | last post by:
I'm creating a class BookType that will store information about books. Each book type can have up to 4 authors. I defined a new type for an array of char: typedef char array4_t; This will...
43
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.