0

I am brand new to SO, and it all looks very helpful.

My code is being used for cryengine, but this seems to be a good all around c++ problem. And lets face it, the official CE forums blow.

The trouble I'm having is accessing the const char* variable of a struct array outside of the scope I'm assigning the variable in.

BuildingManager.h    

class CBuildingManager  {
public:
     struct SBuilding {
        const char* name;
    };

    struct SBuilding buildings[999];

    //string buildingName;
    const char* buildingList[999];

};

BuildingManager.cpp

void CBuildingManager::LoadBuildingXML() {
    int n = -1;

    const char *name;
    const char *value;

    //XML iterator is long and not necessary for example. n++ per iteration. 
    //it just works


                //last part of iterator
                for (size_t j = 0; j < tags->getNumAttributes(); j++) {

                    //Get building name in XML. This works
                    tags->getAttributeByIndex(j, &name, &value);

                    //assign building name to name in struct
                    buildings[n].name = value;

                    CryLog("%s", buildings[n].name);

                    buildingList[n] = buildings[n].name;
                }
            }   
        }
    }
}

void CBuildingManager::LogAction(int x) {

    //x modified by input. also works
    CryLog("%c", buildingList[x]); //this, however, does not

}

So basically, I can print the building name as a string inside of the iterator, and it prints the whole building name (ie. "House")

But when I call LogAction, the building name will only print as a char, and will only show a single random symbol.

How can I convert the name in the struct to a string or otherwise get it to print as a whole word outside of the iterator?

Please let me know if my question is vague or shaky, and I will do my best to clean it up.

-Moose

  • Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Jun 17 '17 at 06:07
  • Hint: what is the lifespan of array of char pointed to by `value` assigned in `buildings[n].name = value;`? – user7860670 Jun 17 '17 at 06:07
  • 1
    *but this seems to be a good all around c++ problem* -- Which is easily solved by using `std::vector` and not `const char *` arrays. – PaulMcKenzie Jun 17 '17 at 06:17
  • @PaulMcKenzie. Thanks for the heads up! Unfortunately, I can only use a `const char *` as its the only way to pull it from the XML. I will look into maybe a conversion – Moose King Jun 17 '17 at 06:42
  • @MooseKing: `%c` is for a single character. `%s` is for a string. Change it inside `LogAction` function. – Azeem Jun 17 '17 at 06:54
  • I would but the engine crashes. It will not let me use it as a string outside of scope. I was able to get a good answer here https://forum.cryengine.com/viewtopic.php?f=15&p=4087#p4087 Looks as if the char is being terminated once it leaves scope, which is something i did not know – Moose King Jun 17 '17 at 07:10
  • @VTT I didn't know there was a life span. This brings new light. Thanks! – Moose King Jun 17 '17 at 07:42

0 Answers0