Adding to char Arrays via loops

Posted on 2004-08-21
Last Modified: 2010-04-01
What I am trying to do is generate lists of files in folders and store them in a char array. However, I dont know how to add a filename on the next loop. How do you add things to a character array?

You cant do it like this:

char ch = new char[];

ch = "hi";

ch = ch + "hi"; // does not work

ch += "hi"; // does not work

I tried using strcat() but my program crashes whenver I use it.

Question by:sint4x
  • 4
LVL 30

Expert Comment

ID: 11862728
>>char ch = new char[];

The above should be the following:

char *ch = new char[123]; //Where 123 is the size of the array that you want
LVL 30

Expert Comment

ID: 11862736
char *ch = new char[123];

strcpy(ch, "Hello ");
strcat(ch, "World");

cout << ch << endl;

Author Comment

ID: 11863891
Ahh, i see the only reason why it keeps crashing is because I didn't specify an array size.

Now how do I reset the size after I add things? (getting a directory list could be very large.)
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 30

Accepted Solution

Axter earned 125 total points
ID: 11864915
>>Ahh, i see the only reason why it keeps crashing is because I didn't specify an array size.

Also, because it was not being assign to a pointer.

>>Now how do I reset the size after I add things? (getting a directory list could be very large.)

I recommend you use a std::vector and an std::string for this requirement.

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> MyArrayOfFileNames;

//To add a name

//To referance a name

std::cout << MyArrayOfFileNames[0] << std::endl;

LVL 30

Expert Comment

ID: 11864926
>>Now how do I reset the size after I add things? (getting a directory list could be very large.)

Otherwise, you would need to create an array of pointer, which would require that you have an idea of the quantity of file names you're going to store in advance.

Then you would have to assign space for each file name, and set it to an each array pointer item.

Expert Comment

ID: 11876027
do you understand pointers?  If not, then this is going to be some black magic:

char* data = new char[256],
      * p = data; // "p" moves along in the "data" array
const char[] filename1 = "hello\n";
const char[] filename2 = "world\n";
strcpy(p, filename1); // put "hello\n" into p (into the data array)
p += strlen(filename1); // this is the pointer magic
strcpy(p, filename2);
p += strlen(filename2);

what the "p += strlen(theStringJustCopied)" does is move p forward in memory, so that it points to the character one after the last character you added.

just remember, even though p is a char*, dont delete it!!  All its doing is pointing to memory that got allocated as part of "data".  DELETE "data" NOT "p"!!!

if you really want to:

void AddToBuffer(char*& inP, const char* inNewString)  // if this doesn't work, try char &*
   strcpy(inP, inNewString);
   inP += strlen(inNewString);

then you can do
char* data = new char[1250];
char * p = data;
AddTobuffer(p, "Hello ");
AddToBuffer(p, "world");

the trick to that working is the "pass pointer by reference", which passes a char*, but does so by reference so that we can change what the pointer is pointing AT.

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question