How can I grow the size of a char array?

char myArray[6];

Assumming that all the current elements are occupied how can I add more empty element to the array to input another value into?

Recommended Answers

All 7 Replies

How can I grow the size of a char array?

That's easy, you can't. Arrays are statically sized and never change. If you want a dynamic array, you'll need to either simulate an array using pointers and dynamic allocation, or switch to a class that does it for you like std::vector.

Here's something I wrote a long time ago: http://www.daniweb.com/software-development/cpp/threads/396591/c-pointers#post1701304 What you want begins around the comment // need bigger array

That said, our pony-loving chum Deceptikon is right; you should use a proper C++ container, and if you want a proper C++ class specially made for char, look no further than std::string

Thanks. But I've been told that I'M not to use classes right now. So I'M pretty much doing C style code in C++. I'll take a look at your link.

we can use malloc () here

You can use malloc on an array that is in the stack.

Learn linked lists. That's one advantage a linked list has over an array.

malloc, realloc will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.