c++ - Copying value of char pointer array to char array -


this simple game , trying learn how apply c++ in existing project (that's how learn best), have experience programming in c# , other high level languages quite new me still. have list of races:

const char* races[] = {    "demon",    "human",    "elf",    "orc",    "aliens" }; 

at 1 point receive registration of user , race selected. save away information of user struct , want save full name of race struct well.

struct user_t {    unsigned int raceid;    char race[16]; }; 

the following code got put values inside struct:

user_t user;  user.raceid = 3; strcpy(user.race, races[user.raceid]); 

this, however, not work. part of game , exception handling horrible (it crashes game without error visible). trying figure out doing wrong. suggestions, perhaps suggestions on other things well?

assuming scope of races array global can avoid copying , this:

struct user_t {    unsigned int raceid;    const char* race; };  user_t user;  user.raceid = 3; user.race = races[user.raceid]; 

you can use std::string mentioned in comments wont avoid copying it. since develop game think performance has higher priority on objectivity of c++ language.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -