Creating array of structs in C++

10,088

Solution 1

Here is a bit modified version that creates edges in the function, and returns a vector containing all created edges.

#include <iostream>
#include <vector>

struct Edge {
    int position[4];
    int average;
};

std::vector< Edge > createEdges(int some_parameters){
    std::vector< Edge > edges( 8 );

    for(int i = 0 ; i < 8; i++){
        const Edge edge{ { 1, 0, 0, 0 }, 10 };

        edges[i] = edge;
    }

    return edges;
}


int main()
{
    auto e( createEdges(5));

    std::cout<<e[0].average<<std::endl;
}

There are few modifications :

  1. Instead of allocating an array of edges, and returning pointer, I return vector
  2. the way edge objects are created, and initialized (I think this is what is asked)

Solution 2

It's C++ use std::vector:

std::vector<Edge> createEdges(int some_parameters){
    std::vector<Edge> edges;

    for(int i = 0 ; i < 8; i++){
        Edge edge;
        edge.position[0] = 1;
        edge.average = 10;
        edges.push_back(edge);
    }

    return edges;
}

Solution 3

What you have here works fine. What I feel you are forgetting, is that when you call

Edge *edges = createEdges(int some_parameters)

that edges variable is a pointer to the first element in your list. If you were to then do:

edges[0]

or:

edges[1]

you would see that these are different values, and you need to now loop through to access the array. Just keep in mind that you do not go passed the end of your array during a loop. This is why most people say to use vectors, because you can use more of the safety features with an iterator.

Solution 4

Welcome to C++! The helper-method for creating instances of a class (a struct in C++ is a class where all the fields are public) is called a constructor. Here is how I would create instances of the edge class, using a constructor.

class Edge {
public:
    int position[4];
    int average;
    Edge();
};

Edge::Edge() //constructor
{
    position[0] = 1;//fill the rest of the edge array in the same manner
    average = 10;
}

int main()
{
    Edge* myEdge = new Edge(); //constructor called
    Edge* myEdges[8];
    for (int i = 0; i < 8; i++)
    {
        myEdges[i] = new Edge();
    }
    return 0;
}
Share:
10,088

Related videos on Youtube

Aerus
Author by

Aerus

Updated on September 15, 2022

Comments

  • Aerus
    Aerus over 1 year

    I'm having a newbie problem in C++.
    I have a struct Edge defined as following:

    struct Edge {
        int position[4];
        int average;
    };
    

    Now I need to create a lot of these structs and I created a helper-method for that, that creates an array of these structs based on some parameters:

    Edge* createEdges(int some_parameters){
        Edge *edges = new Edge[8];
    
        for(int i = 0 ; i < 8; i++){
            Edge edge;
            edge.position[0] = 1; //fill the rest of the edge array in the same manner
            edge.average = 10;
    
            edges[i] = edge;
        }
    
        return edges;
    }
    

    However when I now call: Edge *edges = createEdges(int some_parameters) there is no sensible data in the Edge array (out of scope?).

    I think I'm mixing up some things here but I would prefer if I can make this work without resorting to the vector datastructures. Is this the normal way of handling this or should I declare the edge array myself and pass it to the helper method to fill it up?

    Edit:

    First of all, I want to thank everyone for there comments/hints/tips/advice/... they have helped me to find the problem that I overlooked so easily.
    After I seen the replies that the code should work, I tested the simplified code aswell (something I should have done in the 1st place) and surprisingly, it worked! So then I checked to see why my real code didn't work and the simplified version did.
    My real code looked like this:

    Edge* createEdges(int some_parameters){
         Edge* edges = new Edge[8];
         if(some_parameter != 0){
              //create the edges as in my 1st snippet
              return NULL; //doh, should return edges here !
         } else { 
              return NULL;
         }
    }
    

    The reason why I had not seen that I simply returned the wrong value (NULL) was because the debugger showed me some 0xf6f6f6 addressess with some negative values for edge.position (something I don't quite understand, it should've just show me 0x000000, perhaps I was just imagining things).

    All in all, this was an important lesson in why to never code after 3am, nothing good will come of it!

    • Dan
      Dan over 11 years
      What you have here appears to be correct.
    • Pete Becker
      Pete Becker over 11 years
      Minor tweaks aside, as @Dan said, the code looks right. What's the problem that you're having?
    • andre
      andre over 11 years
      @Gorpik I confirm it and deleted it before yours came up.