# 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 on other sites
Can you use two arrays? Chances are, this will make the code easier to understand.

##### 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 on other sites
For C++, boost::any or boost::variant may be what you're after.

##### Share on other sites
Quote:
 Original post by Azh321I 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 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 on other sites
Quote:
 Effective C++ Item 55: Familiarize yourself with Boost.

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

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by Azh321I 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 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 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 on other sites
Quote:
 Original post by Azh321But 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])

Excuse the terseness, I'm badly tired.

##### Share on other sites
rethink my design? How else could I do it?

##### Share on other sites
I would use boost but im having trouble getting it to work with devcpp

EDIT: I will prob use a linked list but still I need to beable to use variants...

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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627677
• Total Posts
2978590

• 11
• 12
• 10
• 12
• 22