Sign in to follow this  
Falling Sky

Arrays in C/C++

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
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
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[i].c_str());



But lets say I need to add 5 to cell[i], 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
Quote:
Original post by Azh321
But lets say I need to add 5 to cell[i], I turn it into blah and then add 5 to blah, but how do I convert blah back to std::string?

std::stringstream or boost::lexical_cast.
Really, try boost::any. (Pun intended [grin])
Better yet, rethink your design.

Excuse the terseness, I'm badly tired.

Share this post


Link to post
Share on other sites
Ok, I will give you a break on the typing thing. It's valid for a 'calculator' sort of thing since you're effectively implementing a higher-level language, and many such existing languages (think Perl) have loose typing of that sort and it's often pretty convenient.

Don't use atoi(). It returns 0 for an invalid number, and then you have no way to check if there was actually a zero there or some non-numeric thing. Instead, make use of a stringstream (boost::lexical_cast is a wrapper around such stringstream usage). It behaves like any input or output stream, except that it reads to/writes from a string. That also solves the problem of recreating the string quite nicely.


#include <string>
#include <stringstream>
#include <stack>
// Say for example we have a stack of values, and we
// want to multiply the top two, assuming they are numeric.
void product(const stack<string>& mystack) {
string unparsedRHS = mystack.top(); mystack.pop();
string unparsedLHS = mystack.top(); mystack.pop();
stringstream RHSparser(unparsedRHS);
stringstream LHSparser(unparsedLHS);
int parsedRHS, parsedLHS;
char garbage;
if (RHSparser >> parsedRHS && LHSparser >> parsedLHS &&
!RHSparser >> garbage && !LHSparser >> garbage) {
// both were valid ints with no extra stuff at the end
// So now we can create the result value, convert it back to a string
// and store it.
stringstream result;
// This is the only part you have to replace for any binary operation.
// Instead of cutting and pasting all of this wrapper, try to set up
// function pointers or something along those lines to do the variations
// upon this one line:
result << parsedLHS * parsedRHS;
mystack.push(result.str());
} else {
// The values were not valid. Restore the stack
mystack.push(unparsedLHS);
mystack.push(unparsedRHS);
// and throw an exception (or return false, or whatever :s )
}
}




But yeah, constantly converting back and forth will be quite slow. You may want to make use of a discriminated union instead - or better yet, something like boost::any as everyone else is suggesting. That way you can at least keep the converted-to-integer value (assuming there is a valid one) cached for multiple operations.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this