Sign in to follow this  

Array of Arrays?

This topic is 1968 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 cannot for the life of me figure out how to create the following an array of static arrays. below is a picture of what i mean. I want to be able to advanced my pointer 1 byte and hit another string. Is the following even possible?

http://craz.net/Snapper/live/view/?s=r6t

Share this post


Link to post
Share on other sites
I think what you're looking for is char[][256]; I wrote [url=http://www.gamedev.net/topic/618829-array-allocation-from-memory-pool/page__view__findpost__p__4904489]a post[/url] about something similar a while ago... It might be what you're looking for.

Share this post


Link to post
Share on other sites
Have you tried std::vector<std::string>?

If you really need the inner arrays to have an upper limit of 255 characters for some reason, I recommend writing a type for that and putting this type in a vector. At its simplest this could be wrapping a raw array in a struct. A better approach might internalise the details such as ensuring correct handling of the ever troublesome NUL character (assuming you want to use C-style strings).
[code]
struct short_string
{
public:
// std::string-like interface
private:
char data[256];
};

// Later
std::vector<short_string> v;
[/code]

Share this post


Link to post
Share on other sites
You may use std::array (fixed-size array) in conjunction with std::vector (dynamic array): [url="http://ideone.com/W2RGh"]http://ideone.com/W2RGh[/url]

The standard guarantees that both std::array and std::vector are contiguous -- and, hence, support pointer walk-through that you desire:
[url="http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/"]http://herbsutter.co...-be-contiguous/[/url]

If you do that, please be wary of the need to adopt a convention regarding the gaps -- as recommended above, NUL terminator might be a good idea.

However, if you want you can also access each individual word with the usual indexing (MyWords[0], MyWords[1], MyWords[2], ...) which I believe might be easier on the readers of your code :-) Edited by Matt-D

Share this post


Link to post
Share on other sites
Another common thing done for 2D arrays is just use a 1D array and multiply.

[CODE]//Creation:
std::vector<MyType> myArray;
myArray.resize(HEIGHT * WIDTH);
[/CODE]

[CODE]
//Accessing by x and y:
int index = (Y * WIDTH) + X;
myArray[index] = ...;
[/CODE]

The benefit of a 1D array is that, unlike a std::vector< std::vector<type> >, you are sure every row is the same width, and that iteration over the vector is very easy: just iterate over it like a 1D array, but when wanting to access a 2D cell, then index as shown above.

Share this post


Link to post
Share on other sites

This topic is 1968 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.

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