Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Char without Limit

Tags:

c++

char

I'm pretty well versed in C#, but I decided it would be a good idea to learn C++ as well. The only thing I can't figure out is chars. I know you can use the string lib but I also want to figure out chars.

I know you can set a char with a limit like this:

#include <iostream>
using namespace std;

int main()
{
   char c[128] = "limited to 128";
   cout << c << endl;
   system("pause");
   return 0;
}

But how would I make a char without a limit? I've seen chars with * but I though that was for pointers. Any help is greatly appreciated.

like image 434
Lienau Avatar asked Feb 27 '23 02:02

Lienau


1 Answers

You can't have an array without limit. An array occupies space in memory, and sadly there is no such thing as limitless memory.

Basically, you have to create an array of a certain size and write logic to expand the size of the array as you need more space (and possibly shrink it as you need less space).

This is what std::string and std::vector do under the hood for you.

like image 146
James McNellis Avatar answered Mar 03 '23 13:03

James McNellis