c++ - how to use strcpy on dynamic char * buffer -
having trouble allocating dynamic buffer data using strcpy (sigsegv (qt linux))
char ** data = new char *[10]; int current = 0; char temp[20]; std::cin >> temp; strcpy( data[ current++ ], temp); //works(before edit) //i saw sigsegv message on dynamic input //that's why thinking works char * dynamictemp = new char [ 20 ]; std::cin >> dynamictemp; strcpy( data[ current++ ], dynamictemp ); //sigsegv
how should store data? it's knowledges, no need string examples.
you have allocated array of char pointers. need allocate memory each of strings, this:
char * dynamictemp = new char [ 20 ]; std::cin >> dynamictemp; data[ current++ ] = new char [ 20 ]; strcpy( data[ current ], dynamictemp );
Comments
Post a Comment