0

this question is regarding the syntax of an array of array of structs.

I have a struct that takes in two ints:

struct point
{
    int x, y;
};

I have created another struct that takes in 8 of these structs:

//Creating an Array of Array of structs
struct Arraypoint
{
    point variable[8];
};
//Not sure if this is the correct way to do it.

Now, in main, I want to declare an array variable of type Arraypoint with 8 indices, so effectively I will have 8 * 8 = 64 elements of struct point and 128 ints (64 x and 64 y).

Also, how would I access an individual element struct point from the array Arraypoint?

Okay after having declared in main lets say Arraypoint is 2.

Arraypoint arr[2];

How do I initialize the elements without having to type in arr[0].variable[0].x = ... or without using for loops. Why can't I do the following, it doesn't seem to work.

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                       {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }//xy are rand

I have used curly braces in my code, the error returned is missing braces around initializer for type point and too many initializers for type Arraypoint.

4
  • 1
    Might be a good idea to specify which language :) I'm assuming C++ based on the semi-colons at the end of the structs. Commented Dec 6, 2011 at 18:30
  • You should know that (x,y) is not an object of type point. Have you tryed replacing the ()s with {}s, too? Commented Dec 6, 2011 at 18:54
  • yup that doesn't seem to work Commented Dec 6, 2011 at 19:28
  • This seems like C to me, not C++. In C++, we don't use arrays any more Commented Nov 14, 2015 at 9:36

5 Answers 5

4

In C++, you'd just write:

Arraypoint arr[8];

An individual point could then be accessed via:

arr[i].variable[j];

More practically, though, you'd probably be better off using e.g.

std::vector<std::vector<point> >

or writing your own class with an overloaded operator(int i, int j). For example:

class PointMatrix
{
private:
    std::vector<point> m_points;
public:
    PointMatrix() : m_points(64) {}
    point& operator()(int i, int j) { return m_points[8 * i + j]; }
    const point& operator()(int i, int j) const { return m_points[8 * i + j]; }
};

PointMatrix mat;
m(3, 4).x = 23;
Sign up to request clarification or add additional context in comments.

2 Comments

Sure the practicality of the std::vector solution depends on his needs for dynamic size in contrast to consecutive storage. But +1 for mentioning it and the custom class solution.
Thanks :) If fixed-size storage is required, I guess std::array might be a good way to go.
1

got it: ideone.com/ix3hC. Arraypoint::variable has to have it's own { } pair.

struct point
{
    int x, y;
};
#define P {0, 0}

struct Arraypoint
{
    point variable[8];
};
#define V { P, P, P, P, P, P, P, P} 
#define AP { V }  //this is the pair you missed

int main() {
    Arraypoint arr[2] = { AP, AP };
}

Comments

1
struct Arraypoint arraypoints[8];

is what you're after, I think. To use them:

int firstx = arraypoints[0].variable[0].x;

This isn't so pretty though

struct point { int x, y; };
struct point[8][8] arraypoints;

Is probably better? Don't know what exactly you're after though.

Comments

1

To create an array of Arraypoints, you can do:

Arraypoint arr[8];

To access an element:

arr[i]

will return the i'th Arraypoint element

arr[i].variable[j]

will return the j'th point in the element

arr[i].variable[j].x

will return the x coordinate of that point.

Comments

0

So I realized why I couldn't declare my array as such,

Arraypoint arr[2] = {  {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
                   {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)}  }
                    //xy are randomn integer values

its because in my struct declaration of Arraypoint, it takes in 8 elements of type point. So I have to create variables of type point to store(x,y) and then i could store this variable in Array point.

  point point1 = {x,y}, ...;   

  Arraypoint arr[2] = {  {point1,point2,point3,point4,point5,....}  };

Just for anyone in the future who stumbles across the same problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.