简体   繁体   中英

Is there a function that can append to the front of a string without having to create two different char arrays in C?

Concatenating to the end of a string in C is easy:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
    char str[15] = "new ";

    printf("old str = %s\n", str);
    strcat(str, "word");
    printf("new str = %s\n", str);
    return 0;
}

Output:

old str = new
new str = new word

But when I append to the front of an array I do this:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
    char str[15] = "word";
    char newStr[15] = "new ";

    printf("str = %s\n", str);
    strcat(newStr, str);
    printf("new str = %s\n", newStr);
    return 0;
}

Output:

str = word
new str = new word

But is there a function that allows me to append to the front of a string in one line? To be more clear, Is there a way to append to the front of a string without having to create two different char arrays? I just feel that my code would look much cleaner if I could just do this:

semi-sudo code of append function

str = "word";
append(str, "new ");

and when printed would give the following output:

str = word
new str = new word

Something like this would make my life a lot easier since I've been working a lot with file inputs lately.

Thanks!

You can make your own:

#include <stdio.h>
#include <string.h>

char  *strprepend(char str[], char const *prefix)
{
    size_t slen = strlen(str);
    size_t plen = strlen(prefix);
    memmove(str+plen,str,slen+1);
    memcpy(str,prefix,plen);
    return str;
}

int main(int argc, char *argv[]){
    char str[15] = "word";
    printf("str = '%s'\n", str);
    printf("new str= '%s'\n", strprepend(str,"new "));
    return 0;
}
/*
Output:

str = 'word'
new str= 'new word'

*/

If you don't wish to copy strcat 's unsafety, you can add a buffer-size check as well.

Nope, probably you should have a look at the FreeBSD kernel internals to see how they solve the problem with the TCP/IP stack. As they need to efficiently (this means no string copies are done if not necessary) build packets, and they have to add data to the front of it, they allocate some structures (called mbuf s that build using small, preallocated and with several capacities) to build full packets. They also begin filling the data not at the beginning, but they reserve some space in front of it, in order to add it later on, when the amount of data to add is known. This is also done in linux, so it is also a good starting point.

By the way, it would be a good thing you explain your concrete problem... so it is possible to asses you in how to better and efficiently you can build your thing. Think that reallocation and copying data is normally bad habit, and it conduces to low performance and inefficient code. The best approach is to consider how you can develop things without having to move large blocks of memory around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM