Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tonymontana

This is Completely wrong isn't it?...

This topic is 5225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my code requires a lot of global strings (char* ) which i do not want to make global and thats why i tried that ...
char* RequiredStrings(int num)
{
	if(num>5)
	{
		return NULL;
	}
	static char* AllStrings[3];
	*(AllStrings)="Are You Sure?";
	*(AllStrings+1)="Registration Error!";
	*(AllStrings+2)="Creation Error!";
	*(AllStrings+3)="MY CLASS NAME";
	*(AllStrings+4)="MY WINDOW CAPTION";
	switch(num)
	{
	case 1:
		return *(AllStrings);
		break;
	case 2:
		return *(AllStrings+1);
		break;
	case 3:
		return *(AllStrings+2);
		break;
	case 4:
		return *(AllStrings+3);
		break;
	case 5:
		return *(AllStrings+4);
		break;
	default:
		return *(AllStrings);

	}
}
; that must be completely wrong because when we left the function all strings will be destroyed and the pointer char* we get from it will be INVALID.. So Can You Suggest me a solution i will use a lot of char* (i will use some of them rarely onthe otherhand i will use some alot) but i dont want to give them global access (actually i don''t want them to get that much space on ram) And how can i make a routine that creates me the char* i need... the archer who shoots his arrow beyond his target is no more successful than the one who''s arrow didn''t even reach the target.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
well first, unless I am mistaken, there is an error in your code... your char* array looks to be too small...

it''s a scope issue... the char* array is declared locally to that function so when you exit it, the var is no longer accessible.

2 suggestions.. not sure if this first one will work since I program mostly in java and the whole garbage collection has messed up my memory of c and c++... but try passing in a char* into the function and setting that equal to the string you want...

or, since it appears like in this section, speed is not a concern, you could just return the string instead of a point to it....

Share this post


Link to post
Share on other sites
Well, if you have to do it that way, then this should work IMO:


char* RequiredStrings(int num)
{
static char* AllStrings[5] = "Are You Sure?",
"Registration Error!",
"Creation Error!",
"MY CLASS NAME",
"MY WINDOW CAPTION";

if ( num > 4 )
{
return NULL;
}

return AllStrings[num];
}


P.S. No cancel that, I'm not sure if you can initialize a static array of strings like that.



[edited by - Red Ant on June 3, 2004 9:24:05 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
or, since it appears like in this section, speed is not a concern, you could just return the string instead of a point to it....


Hehe, classic C-strings essentially ARE pointers ... sort of. The only way to return one of these from within a function is to return a pointer. Otherwise you''d have to use a string class.

Share this post


Link to post
Share on other sites
First you should tell us which language are you using? C or C++. I'll assume C++. First if you wont to use that function you almost have it wright, when you declare a variable as static inside a function it is created & initialized once and persists through-out all invocations until the program exits.

At the moment your creating it once but reassigning thous constant literal strings but they are not static so they go out of scope. Also you've declared an array of 3 elements but your trying to use 5 elements!. If you wont to do it that then you should use an array initializer, this how it should be like this:


const char* const read_string(int num) {
static const int size = 5;
static const char* const all_strings[size] = {
"Are You Sure?", "Registration Error!",
"Creation Error!", "MY CLASS NAME", "MY WINDOW CAPTION" };

if(num >= 0 && num < size)
return all_strings[num];
else
return 0;
}


But if your using C++ then you would be better of using a collection of std strings and maybe make a functional object that contains either an array or a hashmap of constant strings, then you just pass the functional object around where it's intially constructed in say some application type object or maybe even loaded from a text file.

[edited by - snk_kid on June 3, 2004 9:39:11 AM]

Share this post


Link to post
Share on other sites
You just have a few pointers, no real chars. What''s needed is a 2D array of chars.
char strings[maxlenght][numStrings]
But consider if std::strings are not the solution.

http://www.cis.gsu.edu/~shong/oojokes/

Share this post


Link to post
Share on other sites
quote:
Original post by snk_kid
First you should tell us which language are you using? C or C++. I'll assume C++. First if you wont to use that function you almost have it wright, when you declare a variable as static inside a function it is created & initialized once and persists through-out all invocations until the program exits.

At the moment your creating it once but reassigning thous constant literal strings but they are not static so they go out of scope. Also you've declared an array of 3 elements but your trying to use 5 elements!. If you wont to do it that then you should use an array initializer, this how it should be like this:


const char* const read_string(int num) {
static const int size = 5;
static const char* const all_strings[size] = {
"Are You Sure?", "Registration Error!",
"Creation Error!", "MY CLASS NAME", "MY WINDOW CAPTION" };

if(num >= 0 && num < size)
return all_strings[num];
else
return 0;
}


But if your using C++ then you would be better of using a collection of std strings and maybe make a functional object that contains either an array or a hashmap of constant strings, then you just pass the functional object around where it's intially constructed in say some application type object or maybe even loaded from a text file.



I think i better also mention why i your i've declared a const char* const all_string[size] & returning const char * const.

It means a constant pointer to a constant data what that means is the data is constant and will never change and the pointer always pointers to the same location be it an object or the first element of an array you can't change it to point some where else.

[edited by - snk_kid on June 3, 2004 9:39:40 AM]

Share this post


Link to post
Share on other sites
why not say:

return AllStrings[num];

or
std::string& RequiredStrings(int num) {
static const std::string one("Are You Sure?");
static const std::string two("Registration Error!");
static const std::string three("Creation Error!");
static const std::string four("MY CLASS NAME");
static const std::string five("MY WINDOW CAPTION");
switch(num) {
case 1:
return one;
case 2:
return two;
case 3:
return three;
case 4:
return four;
case 5:
return five;
}


This is awful code but I assume you're not doing this for real, just experimenting.

[edited by - quorn on June 3, 2004 9:50:17 AM]

Share this post


Link to post
Share on other sites
Well - though you did never say it''s for win32, but since the strings like "My window caption" or "My window class" it looks like win32 (stop reading here, if I''m mistaken). So - if you''re on win32 - why not use stringtable resource? LoadString function - it should work well for you.

Oxyd

- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share this post


Link to post
Share on other sites
first of all thanks for your replys
In essence, i even didn''t compile that code.It was just an instance of what i am thinking and i know that i can use static
arrays (but actually this is not the thing i want because as globals ,static arrays will also live as long as program exist..
only difference will be their scope )
weel actually i can use string resource (good idea) but if they are acting like as globals (starts to locate on ram at the beginning and dissepears only when the program exits..thus allocating my precious part of ram)i don''t use them
My Matter is i want to get a string from a array of strings(by a function probably) but
do not want to keep memory for that array of string for program life...(maybe i want the impossible
(Anyway thanks ..)

the archer who shoots his arrow beyond his target is no more successful than the one who''s arrow didn''t even reach the target.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!