Jump to content
  • Advertisement
Sign in to follow this  

How do I add a char to a char array?

This topic is 3998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ihave two veriables char a; char b[256]; need to do something like b = b + a; how can I get this acomplished? Thanks

Share this post

Link to post
Share on other sites
The easy way:

std::vector<char> array;
char 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
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
// 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 = inputStr;
// 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.



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;
// 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!