+ 6
if you want to program in C++ you should follow C++ guidelines. C++ added two keywords to replace `malloc` , `calloc` and `free` which are `new` and `delete` second arrays are fixed-size data structures, Normally you should not need to resize them , you can use other data structures instead of an array if you need to resize your array for an unknown amount of time, here is an example of creating array at runtime #include <iostream> using namespace std; int main() { int size{}; // size of the array cin >> size; string* ptr=new string[size]; for(int i = 0;i<size;i++) { string x{}; cin >> x; ptr[i] = x; } // printing each element of the array for(int i = 0; i < size; i++) { cout << "array at "<< i << " is " << ptr[i] << endl; } delete[]ptr; return 0; }
1st Mar 2022, 10:42 AM
Nima
+ 5
Nice answers Nima If Manav Roy wants to do it the hard way, he can come over & learn it in C. šŸ”ØšŸ’
1st Mar 2022, 11:06 AM
HungryTradie
HungryTradie - avatar
+ 3
C++ is designed on top of C, so that means it supports C features but you should avoid using some of C features and do it in the C++ way! example : how to initialize a variable in C vs C++ // C int x = 0; // C++ int x{}; for further reading check C++ Crash Course https://books.google.com/books/about/C++_Crash_Course.html?id=n1v6DwAAQBAJ&printsec=frontcover&source=kp_read_button&hl=en#v=onepage&q&f=false
1st Mar 2022, 10:57 AM
Nima
+ 3
avoid using calloc and malloc and free and other memory management functions C++ approach is to use new and delete keywords
1st Mar 2022, 11:01 AM
Nima
+ 3
using {} for initializing variables is called brace initialization and it's very common in C++ and is the preferred way Conclusion: Prefer {} initialization over alternatives unless you have a strong reason not to.
1st Mar 2022, 11:07 AM
Nima