-2

Our teacher gave this code and we need to get this code operational.

How can I print the values inside of array?

cout << wizardsCollection->birthYear;

is returns what I gave but wizardsCollection->nameSurname returns empty value.

Here's the rest of the code:

struct Wizard {
    string nameSurname;
    int birthYear;
    string hairColour;
};

struct Wizard *createWizards() {
    Wizard wizardsCollection[3];

    for (int index = 0; index < *(&wizardsCollection + 1) - wizardsCollection; index = index + 1) {
        wizardsCollection[index].nameSurname = "Name and surname of " + index;
        wizardsCollection[index].birthYear = 0;
        wizardsCollection[index].hairColour = "Hair colour of " + index;
    }

    return wizardsCollection;
}

int main()
{
    Wizard *wizardsCollection = createWizards();
    cout << wizardsCollection->nameSurname;
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
hirtevurte
  • 41
  • 8
  • 1
    I recommend [enabling your compiler warnings](https://godbolt.org/z/eEbnsxMnq). It will also help your question if you can explain what "I can't [print]" and "not working" means. – Drew Dormann Feb 08 '22 at 19:36
  • _Please don't give me bad comment. I want to print all values of array but I can't."_ well i'll give you _"bad comment"_: What's actually happening? You're unclear as can be. Voted to close, downvoted. – πάντα ῥεῖ Feb 08 '22 at 19:39
  • I can't find enough resources on my own language. Also I think my English not enough for find resources. – hirtevurte Feb 08 '22 at 19:46
  • You cannot return an array in C++. The array [will decay to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) and then go out of scope leaving the program with a pointer to an invalid object. Using that pointer to access the invalid object [could do anything](https://en.cppreference.com/w/cpp/language/ub) and in this case that anything is an empty string. You could also get exactly what you expected, making it hard to spot the mistake sometimes, or [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html) – user4581301 Feb 08 '22 at 19:47
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). These books are all in English and the links are to English versions, but you should find many of them translated to a wide variety of other languages. – user4581301 Feb 08 '22 at 19:48

1 Answers1

0

You have a huge problem here. wizardsCollection is created on the stack. As soon as the function returns, that stack memory goes away. Your pointer will be pointing into empty space. You need to use a std::vector for this, to manage the memory.

#include <iostream>
#include <string>
#include <vector>

struct Wizard {
    std::string nameSurname;
    int birthYear;
    std::string hairColour;
};

void createWizards(std::vector<Wizard> & wizardsCollection) {
    wizardsCollection.clear();
    for (int index = 0; index < 3; index++ ) {
        wizardsCollection.push_back(Wizard({
            "Name and surname of " + std::to_string(index),
            0,
            "Hair colour of " + std::to_string(index)
        }));
    }
}

int main()
{
    std::vector<Wizard> wizardsCollection;
    createWizards(wizardsCollection);
    std::cout << wizardsCollection[0].nameSurname << "\n";
}

There are other ways to do it, but this models what you had.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30