Question:
C++ dynamic string array: how can i declare a dynamic sting array in c++ without pointers?
2013-05-26 10:28:09 UTC
Can i use,
char a[] for declaring dynamic string array?
Please tell me the simple way...
Five answers:
_Object
2013-05-26 12:31:45 UTC
Not like that, you cannot.

The array size must be static and cannot vary. I would use something like

std::vector StringContainer;

Vector takes care of allocating more strings for you after a call to std::vector::resize(), while std::string will resize itself to hold whatever is assigned to it.

Pointers are necessary when dealing with dynamic allocation, but the standard containers will take care of this for you behind the scenes.
?
2016-12-12 22:22:53 UTC
Dynamic String Array
Nigel
2013-05-26 11:17:14 UTC
Yes, but its only a play with words.



If you were to create a String class. You could define both public and private access operators. The private access operators would do the real nitty gritty stuff with dynamic memory allocation and pointers, while the public access operators would provide a more simple interface to add(), delete(), length() operators.



So the programmer who gets to write the String class would be unable to avoid the pointer programming. However, the programmer who gets to use the String Class would have a more simple solution.



The String class programmer can provide as many simple public access operations that they see fit. They could have one called change_lenght() that provides a public interface to the privately hidden dynamic memory allocators/destructors written with pointers and rigorously written calls to malloc(), free() and realloc().
?
2016-10-29 01:10:14 UTC
Arrays are in actuality guidelines considering they generally are defined to have greater archives that a single address in RAM can carry. So, sure of direction it incredibly is available. (without defining a secondary pointer) char my_array[5] = "1234"; // we've an array with 5 bytes dispensed, which contain the NULL my_array[3] = 5; // we point out which place in our array to alter. for this reason place 3(it incredibly is 4 considering we initiate counting with 0) output: 1235
kenzo2047
2013-05-26 10:46:39 UTC
You cannot. If the array is dynamic, then that means is it allocated using dynamically-allocated memory, which implies that it can only be accessed via a pointer.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...