Sign in to follow this  
kingpinzs

How do I add a char to a char array?

Recommended Posts

The easy way:

std::vector<char> array;
char c;

array.push_back(c);


The unsafe way:

char unsafeArray[256];
char c;

unsafeArray[index] = c;


If the array is full and you are trying to add to it (ie, index > 255), you will need to allocate a new larger array, copy over the existing data from the old array, and then insert the desired char.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Wow... two posts sugesting std::vector?
how about std::string?


The OP didn't say anything about the char array representing a "string", so inferring it (and thus suggesting std::string) would be like an admission of defeat, i.e. an admission that the idea of using a char[] to represent a "string" makes any kind of sense. ;)

Share this post


Link to post
Share on other sites
what I need it to do is char gets a single charicter from the input checks it and if it passes adds it to the char array or some would call it a c string.

then I make the input char array the new char array.

Share this post


Link to post
Share on other sites
Oldschool cstrings might not be your best bet here. Those character arrays cannot be dynamically expanded. Once you set it to a specific size (256 in your case) you cannot add a character at or passed array index 256.
Your best bet (and easiest) is to use std::string.


#include <string>

/*
...
*/


std::string inputStr;
char character;
// get character and put in character variable
// check character
if( characterPasses ){
inputStr.append( character );
}

// Then if you want to access the std::string like an array, you can
inputStr[4]; // gets the 5th (at index 4) character
// you can also do other fun things that you cannot do with old
// character school arrays
inputStr.length()
// gets #chars long, also the size of the array if u like it that way




Otherwise, if you really don't want to use string, you will need to make your initial character array large enough OR manually expand it (which can be slow).


// something like this might be big enough
char *inputStr = new char[1024];
// that would allow for a string that is 1024 characters long
// but be sure to delete it like so
delete[] inputStr;



// if you wanted to expand it, you could do
int smallSize;
char *inputStr = new char[smallSize];
int expandedSize;
// assume that you know the above variables values
// so create the new char array of the expanded size
char *newInputStr = new char[expandedSize];
// now copy the values from the old cstr
// note this is not the fastest way to do it, but its easiest for now
for( int i=0; i<smallSize; i++ ){
newInputStr[i] = inputStr[i];
}
// now, swap the character array pointers, so input string is the expanded array
char *oldCharArrayDeleteMe = inputStr;
inputStr = newInputStr;
// now delete/clear the old data/references
newInputStr = NULL;
delete[] oldCharArrayDeleteMe;
/* inputStr is now a larger array, it is larger by expandedSize-smallSize
again, since I have used the new operator, it will need to be deleted eventually. Note that the new operator is not necessarily needed, but it probably will be.*/

delete[] inputStr;




I hope this helps. Also, I cannot stress enough that you should use std::string.

Goodluck,

-Sevans

Share this post


Link to post
Share on other sites
this is the beginners' forum. Thus using some sort of char array to represent a string is out of the question. Use std::string.

Share this post


Link to post
Share on other sites
To bring it together.

if you have:

char a;
char b[256];

As mentioned, if you want to add "a" at b[256] (one past end), it can't be done. If you have say 110 items currently in b, and you want to add a to be the 111th item. Then first, you have to KNOW that you have 110 items in it. This means if its a normal byte array, you must have a third variable, something like "unsigned length_b = 0;" and that variable would have 110 in it, so you would do something like:

b[length_b++] = a;

if you are using null terminated ascii strings (aka C strings), then you use strlen(b) to find the length at any given time:

unsigned length = strlen(b);
if(length < MAX_LEN)
{
b[length] = a;
b[length+1] = 0;
}
else
{
// couldn't add;
}

VERY IMPORTANT, if you ware using NULL-terminated strings, then all allocations need to be MAX_LENGTH+1 because you need room for the null terminator, like this:

char b[MAX_LENGTH+1];

of course std::vector<char> is better for a non-null terminated char array and std::string is usually better for replacing a null-terminated byte array.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this