I apologise if I'm completely misunderstanding C++ at the moment, so my question might be quite simple to solve. I'm trying to pass a character array into a function by value, create a new array of the same size and fill it with certain elements, and return that array from the function. This is the code I have so far:
char *breedMutation(char genome []){ size_t genes = sizeof(genome); char * mutation = new char[genes]; for (size_t a = 0 ;a < genes; a++) { mutation[a] = 'b'; } return mutation; } The for loop is what updates the new array; right now, it's just dummy code, but hopefully the idea of the function is clear. When I call this function in main, however, I get an error of initializer fails to determine size of ‘mutation’. This is the code I have in main:
int main() { char target [] = "Das weisse leid"; //dummy message char mutation [] = breedMutation(target); return 0; } I need to learn more about pointers and character arrays, which I realise, but I'm trying to learn by example as well.
EDIT: This code, which I'm trying to modify for character arrays, is the basis for breedMutation.
int *f(size_t s){ int *ret=new int[s]; for (size_t a=0;a<s;a++) ret[a]=a; return ret; }
std::strings and/or specially designed classes rather than plainchar-arrays.