Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


passing 2D array to struct


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 hikarihe   Members   -  Reputation: 243

Like
0Likes
Like

Posted 02 March 2013 - 05:31 AM

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



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8429

Like
2Likes
Like

Posted 02 March 2013 - 05:39 AM

Declare your filenames field as follows:

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


#3 hikarihe   Members   -  Reputation: 243

Like
0Likes
Like

Posted 02 March 2013 - 05:44 AM

It compiles, thank u!



#4 cozzie   Members   -  Reputation: 1654

Like
0Likes
Like

Posted 02 March 2013 - 10:25 AM

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)



#5 hikarihe   Members   -  Reputation: 243

Like
0Likes
Like

Posted 02 March 2013 - 12:23 PM

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?



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5407

Like
1Likes
Like

Posted 02 March 2013 - 12:30 PM

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

#7 cozzie   Members   -  Reputation: 1654

Like
0Likes
Like

Posted 02 March 2013 - 04:02 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS