Jump to content
  • Advertisement

Archived

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

RizMan

Passing an array of strings

This topic is 5769 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

I got this problem which I hope you can help me with. What I want to do is read a file where I store the path of the texture files I use in my game. It has following structure: Number of textures texture1.bmp texture2.bmp . . . Now I have this function where I read the number of textures into the variable "numTextures". numTextures is passed back to the main function. Then in the main function I create an array of strings with char **textureFiles; textureFiles = new(char*[numTextures]); Now I call following function: bool LoadTextureFile(char **textureFiles, int numTextures); This function is supposed to read through the textures.txt file and store the paths to the textures in textureFiles. It does it well, but when coming back to the main function, all of the value stored in textureFiles are gone. So I figure I am passing the array of strings as value, but I don't seem to can figure out how to pass it by reference. Everything I tried doesn't seem to work. Or is it something completely different? Or am I just stupid? I searched through google, but couldn't find a similar example. Here is the code, maybe you'll understand better what I mean:
      
int LoadTextures()				
{
	int numTextures;
	numTextures = GetNumTextures();
        char **textureFiles;
	textureFiles = new(char*[numTextures]);
	if(!LoadTextureFile(textureFiles, numTextures)){
		return false;
	}
//...

}
bool LoadTextureFile(char **textureFiles, int numTextures){
	FILE *fp;
	char name[30] ="";
	//...

	fscanf(fp, "%d", &numTextures);   
	while(fgets(name, 30, fp)){
		textureFiles[i++] = name;
	}
	fclose(fp);
	return true;
...
}

      
[edited by - rizman on February 2, 2003 9:56:58 AM] [edited by - rizman on February 2, 2003 9:59:08 AM]

Share this post


Link to post
Share on other sites
Advertisement
Don''t make life hard for yourself. Arrays are evil, so use std::string instead of char arrays and std::vector instead of general arrays, and you''ll neatly sidestep the problems of passing arrays of c-strings. It''s easy to pass vectors of std::strings.

Share this post


Link to post
Share on other sites
I figured that too, but I would need to recode a lot of stuff. Though considered that I''m trying to figure this out for about 2 hours now, I would have done the recoding already

I''d be interested in how to do this anyway. I mean with arrays.

Share this post


Link to post
Share on other sites
Aarrrrg, just because I was indeed to lazy to do some recoding, I lost over 2 hours. Now I changed the stuff to using vector and it works great. This took me about 20 minutes. Foolish me.

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!