I need to resize char array[size]at char array[new_size]runtime.
char array[size]
char array[new_size]
How can i do this?
ok, thanks for all the answers, I fixed my problem by simply creating a new space for the new char throwght pointer array ... thanks
If you used std::vector<char>, not arrays, then the desired function will be just another method for this type.
std::vector<char>
, char array[size] malloc 'ed.... realloc
malloc
realloc
( OpenBSD):
newsize = size + 50; if ((newp = realloc(p, newsize)) == NULL) { free(p); p = NULL; size = 0; return (NULL); } p = newp; size = newsize;
.
- :
class HangUpGame { char *palabra; size_t palabra_size; public: HangUpGame(){ palabra = new char[DEFAULT_SIZE]; palabra_size = DEFAULT_SIZE; } virtual ~HangUpGame(){ delete [] palabra; } void Resize(size_t newSize){ //Allocate new array and copy in data char *newArray = new char[newSize]; memcpy(newArray, palabra, palabra_size); //Delete old array delete [] palabra; //Swap pointers and new size palabra = newArray; palabra_size = newSize; } };
- STL. , , ( STL ).
, , :
char* array = (char*)malloc(sizeof(char) * number_of_chars_in_word);
Source: https://habr.com/ru/post/1716442/More articles:LuaJit increases stack / heap size - stackКак сделать XCode Run Script Запуск фазы сборки, если сборка ломается? - scriptingHow can you go forward / backward at the file level with Xcode 3.2? - xcodeHow can I launch an Android app automatically when an email arrives? - androidHow to convert NSString to char? - charCRUD template for urls.py, passing an object from a URI to view - genericshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1716444/sql-left-join-2-foreign-keys-to-1-primary-key&usg=ALkJrhhYoOAtD6miTjUqpupzkfMBdlN4NALoading a loadable JavaScript image when making an ajax call - jquerycheck if username exists - phpGetting the "final" prepared statement from MySqlCommand - mysqlAll Articles