#### Archived

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

# 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.

## 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 numTextures;
numTextures = GetNumTextures();
char **textureFiles;
textureFiles = new(char*[numTextures]);
return false;
}
//...

}
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 on other sites
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 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 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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633388
• Total Posts
3011620
• ### Who's Online (See full list)

There are no registered users currently online

×