473,811 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 internationaliz ed. So the
following
function definition, I thought, would be good:

char* translateText(c har* 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(c har* text) {
Locale locale;
locale->translate(&tra nslated, 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 1248

<lo************ @lollisoft.de> skrev i en meddelelse
news:11******** *************@o 13g2000cwo.goog legroups.com...
Hi,

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

char* translateText(c har* 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(c har* text) {
Locale locale;
locale->translate(&tra nslated, 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
3474
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 hold 25 characaters I declared a 2 dimensional array of char in BookType:
43
3430
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
7801
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 of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
18
4070
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
3291
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
9728
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
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10402
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
10135
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
6890
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
5554
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.