user749681
user749681

Reputation: 1

Concatenating two const char * in C

I am working with the const char * type in C. Due to the libraries I am using I am trying not to use or include other libraries (namely cstring). The function I am working in is passed a buffer of type const char *. I want to append a finite message to the end of this buffer.

For example:

functionA (const char *buffer){
    char *message = "hello world";
    //code appending message to the end of buffer
    //buffer now contains its original contents plus "hello world" at the end.
}

I am thinking the best approach is to create a temporary variable to take in the data from the buffer, and then add the message. Once that is done, reassign the buffer pointer to the temporary variable's reference (as I am trying to do this with the least amount of alterations to the function, to reduce the chance of a compile error).

Upvotes: 0

Views: 8995

Answers (9)

Adit Ya
Adit Ya

Reputation: 771

I guess if you know the memory required of the appending message, then we could use memcpy.

somthing like ..

main() { ...

u32 *new_ptr=NULL;
total_len=strlen(previous_msg)+strlen(appending_msg);
new_ptr=malloc(total_len);
memcpy(new_ptr,previous_msg,strlen(previous_msg));
memcpy(new_ptr+strlen(previous_msg), appending_msg, strlen(appending_msg));

... }

The new_ptr will now contain the entire message.

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 177554

Most have mentioned two problems. I'll add a third:

  1. buffer points to const char, so you can't write to it.
  2. buffer is passed by value, so reassigning it won't be visible outside the function.
  3. You have no idea how big buffer is, so if you did write to it, you could cause a buffer overflow.

What you may want (untested) is:

char* functionA(char* buffer, size_t maxlen)
{
    char *message = "hello world";
    if(strlen(buffer) + strlen(message) >= maxlen)
        return NULL;
    return strcat(buffer,message);
}

The length of the two strings must be less then maxlen to allow a nul termination.

strlen and strcat are trivial to implement if you refuse to use them.

Upvotes: 2

orcmid
orcmid

Reputation: 2638

You can find the end position of buffer[] and use that as a destination for a copy of message[]. Any text-book scanning of a string for the terminal '\0' will do, although you want to make sure you don't run off the end of buffer[].

And that reveals a bigger problem. If you have no idea how full buffer is and whether or not your movement of the additional 12 characters (counting the automatically-supplied '\0' on the end) will cause a buffer overflow or not, you need some way to know what the capacity of buffer[ ] is so your code can operate defensively.

Also, although it is safe to make const char *message = ..., the const char *buffer seems a bit odd if you intend to alter it. While the C compiler might not catch that, it is really a violation of the contract implied by using const char *buffer. In addition, a caller is permitted to implement the buffer[ ] in read-only storage although I assume you are the only user of functionA.

So you might want to look at how to change the parameter list and define the interface to this function so it can defend against buffer-over-runs, badly-initialized buffer[ ] strings, etc.

Upvotes: 1

jjwchoy
jjwchoy

Reputation: 1908

Once that is done, reassign the buffer pointer to the temporary variable's reference

This will only change what buffer is pointing to inside the function. The pointer you passed into the function call will not be changed.

Upvotes: 0

Cory Nelson
Cory Nelson

Reputation: 29981

The whole point of a const char is to be constant -- you're not supposed to modify it. You must place the resulting string in a new buffer, like this:

char* combine (char *new_buffer, const char *buffer, const char *message)
{
    return strcat(strcpy(new_buffer, buffer), message);
}

This copies the string into a new buffer then concatenates the message to it.

Upvotes: 0

Shea Levy
Shea Levy

Reputation: 5415

I think the answers to Concatenating strings in C, which method is more efficient?, particularly Concatenating strings in C, which method is more efficient?, will have what you're looking for.

But you'll need to change your arguments a bit for sure. You won't be able to change the buffer in that example, since it's declared const.

Upvotes: 1

Mel
Mel

Reputation: 6157

You can not append to const char *. Because it's constant.

Upvotes: 4

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215201

If it's at all possible to avoid concatenating them, don't. For example if you're going to send them as output, just send one then the other. If you want to concatenate them you need to either get real about using C++ and not treating it like "C with crappy annoying restrictions", or do it the C way which would involve allocating an array of char large enough to hold both strings and copying them into place (preferably with snprintf).

Whichever way you go, if you want to concatenate strings, you have to allocate memory, and you have to be prepared for the possibility that allocation will fail.

Upvotes: 2

Himadri Choudhury
Himadri Choudhury

Reputation: 10353

Try using strcat(). strcat

Upvotes: 0

Related Questions