passing 2D array to struct

Started by
5 comments, last by cozzie 11 years, 1 month ago

how do you do it?

I have this array:


char textureFileNames[ NUMBER_OF_TEXTURES ][ 48 ];
 

and:


// this is what I want
examplestruct.filenames = textureFileNames;
 

  • how do I declare the struct member so that it allows me to pass the address of my array to the member?
  • then how do i assign the address of the 2D array to the member?

any help appreciated happy.png

Advertisement

Declare your filenames field as follows:

struct myStruct {
    char (*filenames)[48];
};

It compiles, thank u!

Hi, never the less, you could do it maybe even easier making filenames a std::string fienames[NUMBER_OF_TEXTURES];

also easy to assing, like:

filenames[x] = "something";

filenames[x] += somevar; etc.

this gives great flexibility in my opinion, compared to using array of chars

(I use it to choose the right filename for effects, based on shadermodel, number of lights etc)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Hi, never the less, you could do it maybe even easier making filenames a std::string fienames[NUMBER_OF_TEXTURES];

also easy to assing, like:

filenames[x] = "something";

filenames[x] += somevar; etc.

this gives great flexibility in my opinion, compared to using array of chars

(I use it to choose the right filename for effects, based on shadermodel, number of lights etc)

is this allowed in C? I've never used std::string before but I think it's for C++ right?

Also isn't the string vulnerable to being overwritten by other data since you don't explicitly specify its size?

Yeah, that is C++ only. It really makes things easier though, string handling in C is a pain.

C++ std::strings are automatically resized so overwriting isn't a problem.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Sorry, it's C++ indeed.

Don't know if you have other mandatory reasons to stay with C, if not, I'd go for std::string

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement