-3

i want to create a dynamic string array without using vectors or pointers.is that possible ??

several candidates are stored in the " Ausgabe " i want to copy all this Element in the " Ausgabe " to the String Array and later expand this array. in the code Below i only get the last Element and the others are lost. is there any solution to get all the Element stored in the array and output it ?

string myArray[5];

const int len = (sizeof(myArray) / sizeof(myArray[0]));

myArray[0] = ausgabe;

const int x = 5;


const int y = sizeof(myArray) / sizeof(myArray[0]); // =5

string neuArray[x + y] = {};


copy(begin(myArray), end(myArray), begin(neuArray));

for (int i = 0; i < x + y; i++) {
    cout << neuArray[i] << endl;
}
El ayoub
  • 1
  • 1
  • 2
    Without using vectors, yes (but why?). Without using pointers, no. What is `ausgabe` declared as, and how is it populated? You are only putting 1 element in `myArray`, but you are copying 5 elements from `myArray` into `neuArray`. Please provide a better [mcve] – Remy Lebeau Aug 30 '23 at 19:34
  • 1
    Side note: `(sizeof(myArray) / sizeof(myArray[0])` use `std::size(myArray)` instead. Simpler and safer. The former screws up hilariously at runtime when you pass it an array that has decayed to a pointer because the pointer lost the array's size. Feed a decayed array to `std::size` and you get a compiler error so you know right away you have a mistake. – user4581301 Aug 30 '23 at 19:38
  • 2
    Note that this works *only* because `sizeof` is a compile-time operation, and that makes `x` and `y` compile-time constants. Once either of them becomes merely a run-time constant (like when they are passed as function arguments) then what you have is a variable-length array, and [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). – Some programmer dude Aug 30 '23 at 19:42
  • *i want to create a dynamic string array without using vectors or pointers.is that possible ??* -- That's like asking "I would like to play football by running on my knees. Is that possible?". Yes, it is possible, but why would you want to do this? – PaulMcKenzie Aug 30 '23 at 20:57
  • The classical *dynamic* allocation is to use `new`: `std::string * p_neuArray = new std::string [x + y];`. – Thomas Matthews Aug 30 '23 at 21:03
  • @RemyLebeau Ausgabe is a string variable – El ayoub Aug 31 '23 at 05:38

0 Answers0