Old fart here, still a horrible newbie.

Tonight, I've actually got a working solution for my problem, but the book I'm reading is unclear about something, and I'm having trouble getting Google to give me the the exact answer I want. Given,

//Header junk
//CandyBar struct
int main()
{
    CandyBar * pt = new CandyBar[3];
    //This init works
    pt[0].brand = "Three Musketeers";
    pt[0].weight = .33;
    pt[0].calories = 200;
    //This init does not work
    pt[1] = {"Hershey's", .45, 225};
    //input, output already work, hallelujah
    delete pt;

    return 0;
}

...how do I push bracketed input into a new struct array like I'm failing to do with pt[1]? Banging out multiple-row initializations is really clumsy. If, for some reason, the answer is, "don't do that," I'll accept that, too.

Dani AI

Generated

Short answer: the brace form you tried is a compile-time initializer (works when the array or object is being created). As pointed out, older compilers (pre-C++11) do not permit using a braced initializer on the right-hand side of an assignment. Modern C++ (C++11 and later) added list-initialization which makes that syntax valid in more places — see list-initialization for the rules.

Practical approaches that work in real code:

  • Give the struct a constructor and assign a temporary. This is the cleanest, portable way and plays nicely with std::string for the brand:

    struct CandyBar {
        std::string brand;
        double weight;
        int calories;
        CandyBar(const char* b = "", double w = 0.0, int c = 0)
          : brand(b), weight(w), calories(c) {}
    };
    
    pt[1] = CandyBar("Hershey's", 0.45, 225);
  • If you cannot add a constructor, return a fully populated temporary from a small factory function and assign that:

    CandyBar make_bar(const char* b, double w, int c) {
        CandyBar x;
        x.brand = b;
        x.weight = w;
        x.calories = c;
        return x;
    }
    
    pt[1] = make_bar("Hershey's", 0.45, 225);
  • Prefer containers over manual new[]/delete[]; use std::vector and emplace_back (or push_back) to avoid manual memory management and get clearer code.

Finally: fix the memory release (use delete[] for arrays allocated with new[]), and prefer std::string for text members to avoid pointer lifetime problems. If you want the brace-assignment you originally tried, enable a C++11-or-later mode in your compiler (e.g., GCC/Clang: -std=c++11) and consult the cppreference list-initialization page above for details.

Recommended Answers

All 2 Replies

kind like this:

CandyBar cb = {"Hershey's", .45, 225};

Only works at initialization.

Line 11. is already too late

this works:

CandyBar cb[3] = { {"Three Musketeers", .33, 200}, 
                                        {"Hershey's", .45, 225}, 
                                        {"Mars", .45, 2250} };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.