Jump to content
  • Advertisement
Sign in to follow this  
Falling Sky

Arrays in C/C++

This topic is 4782 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 have an array but I need to beable to use it for ints and C style strings but arrays only allow one type...any way to get around this? I was thinking of maybe using an array of strings but just atoi() it when its numbers. Would that work?

Share this post


Link to post
Share on other sites
Advertisement
A possibility is the use of unions. That way you can store either an int or char*. Like:


union {
int i;
char* c;
};



You could also hack it a bit and cast the char pointer to and int and back to char* again when you need it. Its all perfectly possible with c/c++ :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Azh321
I have an array but I need to beable to use it for ints and C style strings but arrays only allow one type...any way to get around this?


Why do you think you need this?

When you go to grab something out of the array, how do you plan to know whether it is an int or a string?

And why are you using C-style strings at all anyway? :)

Share this post


Link to post
Share on other sites
I would use boost but I cant even get it working so does anyone have another alternative? Think of the array as memory, it needs to beable to hold strings and numbers.

Share this post


Link to post
Share on other sites
Quote:
Effective C++
Item 55
: Familiarize yourself with Boost.


Just try it. We're glad to help you out with that one. What are your problems? :P

Could you please explain in more detail what you are trying to achieve? Perhaps we can find another way...

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Azh321
I have an array but I need to beable to use it for ints and C style strings but arrays only allow one type...any way to get around this?


Why do you think you need this?

When you go to grab something out of the array, how do you plan to know whether it is an int or a string?

And why are you using C-style strings at all anyway? :)


Quoted, as I concur.

And really, just post the code. Just explain what you're trying to do. We were all beginners, and won't poke fun at horrible code or terrible ideas. Without context, we cannot offer effective alternatives, or help you to the best of our ability.

Share this post


Link to post
Share on other sites
Why dont you just have an array of structs, and the struct can have as many different types as you want.

Share this post


Link to post
Share on other sites
Im writing a simple interpreter for my friends bytecode and it uses an array for the data storage (it calls them cells). But the cells can hold strings or numbers since there variants...I cant really have 2 arrays as that would add a ton more repeatative commands to the bytecode.

I did try doing something like:

blah = atoi(cell.c_str());



But lets say I need to add 5 to cell, I turn it into blah and then add 5 to blah, but how do I convert blah back to std::string?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!