开发者

Concatenating two const char * in C

I am working with the const char * type in开发者_如何学Go 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).


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


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.


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.


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.


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.


Try using strcat(). strcat


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜