Concatenate char to char*

I am trying to concatenate a char to the end of a char*. I have a char* result that i want to add a character to the end of it. I'm not sure if I can do this with strcat.

Any Help?
Thanks.
you can just add it in, but if the array that char * points too isn't large enough, then you'll have to declare a new one... what code do you currently have?

- edit: when I say add it in, I mean, find where on the array to put it and put it there
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
char character = 0;
char* result;

char* nextToken()
{
    memset(result, 0, 256);
    character = fin.get();

    if (character == fin.eof())
    {
        //donothing
    }
    else if (isspace(character))
    {
       nextToken();
    }
    else if (isalpha(character))
    {
        while (isalpha(character))
        {
            result = strcat(result, character);
            character = fin.get();
        }
        fin.unget();
    }
Won't memset write the value 0 over 256 random bytes of memory there, since result is a pointer that has never been set to point at allocated memory? I'd expect to see a segfault there, if you're lucky, and if you're unlucky you'll trash something and not notice.

Also, strcat takes two char* parameters as input, not a char* and a char.
Last edited on
I used to have result as a char array, but changed it to a pointer. I guess ignore that for now.
My main issue is that I want to concatenate a char to a char*. How would i achieve that?
Instead of

result = strcat(result, character);

try

strcat(result, &character);

character is the variable itself, &character is the address of character.

It is up to you to make sure that result points to enough space for your concatenation.
Last edited on
I replaced that and I do not get any compiler errors. However I get a segfault...

This is the code I have now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ifstream fin;
char character;
char* result;

char* nextToken()
{
    character = fin.get();

    if (character == fin.eof())
    {
        //donothing
    }
    else if (isspace(character))
    {
       nextToken();
    }
    else if (isalpha(character))
    {
        while (isalpha(character))
        {
            strcat(result, &character);
            character = fin.get();
        }
        fin.unget();
    }
}

That's because you never set the value of result to any memory you have allocated. You are writing over the top of random memory.

You need to allocate yourself some memory, and then set the value of result to be equal to the address of the start of the allocated memory.

You could allocate the memory like this:

char someAllocatedMemory[300];

which would give you space for 300 char values, or like this

char* p1 = new char[300];

which would also give you space for 300 char values; if you chose the second option, you would have to remember to delete when you are done with it.

Then, either replace result in your code with a pointer that has the value of the address of the start of the memory you allocated (in my example code, the value of p1 is the value of the address of the start of the allocated memory), or set the value of result to the address of the start of your allocated memory.
Last edited on
strcat expects null terminated cstrings. strcat(result, &character); will write garbage.
1
2
3
4
5
6
7
8
strncat(result, &character, 1);

size = strlength(result);
result[ size ] = character;
result[ size+1 ] = '\0';

std::string result;
result += character;
So it does. Oop.

I maintain that all that junk I said about how result pointed to unallocated memory was true, but yes, I completely missed that it would all break at strcat :( ne555 is bang on the money. Listen to him :)
Last edited on
There's another, simpler way:

1
2
3
4
5
char* strcatc( char* s, char c )
  {
  char cs[ 2 ] = { c, '\0' };
  return strcat( s, cs );
  }

Again, this assumes that s is big enough to handle one extra character. For example:

1
2
3
/* good */
char message[ 50 ] = "Hello world";
puts( strcatc( message, '!' ) );
1
2
3
/* bad */
char* message = "Hello world";
puts( strcatc( message, '!' ) );

Hope this helps.
you probably should take things out of global scope ... plus if you want to use a pointer, then allocate and keep track of the size and what is used. You can use something to keep track and that would eliminate some headache for you and just append a null char at the end when done. Plus you can reallocate memory to the pointer if you need more since you are using them anyway.
Last edited on
Topic archived. No new replies allowed.