简体   繁体   English

连接 3 个字符串并返回指向新字符串 C 的指针

[英]Concatenate 3 strings and return a pointer to the new string C

I am wondering if anyone could help me, I am trying to concatenate 3 strings and return a pointer to the new string.我想知道是否有人可以帮助我,我正在尝试连接 3 个字符串并返回一个指向新字符串的指针。 I can't seem to figure out how to do this using strncat instead of strcat and strncpy instead of strcpy.我似乎无法弄清楚如何使用 strncat 而不是 strcat 和 strncpy 而不是 strcpy 来做到这一点。 I am only learning c, so any help wil.我只学习 c,所以任何帮助都会。 be greatly aprecaited.不胜感激。

char *
concatenate(char *a, char *b, char *d)
{
  char str[80];
  strcpy (str, a);
  strcat (str,b);
  strcat (str,d); 
  puts (str);

  return (NULL);
}

You cannot do it this way, you would return a pointer to a local array that is no longer valid once the function returns, furthermore, you do not check for buffer overflow.您不能这样做,您将返回一个指向本地数组的指针,该指针在函数返回后不再有效,此外,您不检查缓冲区溢出。

Here is a quick and dirty version that allocates memory:这是分配内存的快速而肮脏的版本:

#include <stdlib.h>  
#include <string.h>

char *concatenate(const char *a, const char *b, const char *c) {
    return strcat(strcat(strcpy(malloc(strlen(a) + strlen(b) + strlen(c) +  1,
                                a), b), c);
}

Here is a more elaborate version using memcpy and testing for malloc failure:这是一个使用memcpy并测试malloc失败的更详细的版本:

#include <stdlib.h>  
#include <string.h>

char *concatenate(const char *a, const char *b, const char *c) {
    size_t alen = strlen(a);
    size_t blen = strlen(b);
    size_t clen = strlen(c);
    char *res = malloc(alen + blen + clen + 1);
    if (res) {
        memcpy(res, a, alen);
        memcpy(res + alen, b, blen);
        memcpy(res + alen + blen, c, clen + 1);
    }
    return res;
}

It should be more efficient since it does not perform the extra scans strcpy and strcat do, but only careful benchmarking can prove if it is a real improvement over the simple version above.它应该更有效,因为它不执行strcpystrcat所做的额外扫描,但只有仔细的基准测试才能证明它是否是对上述简单版本的真正改进。

If you need to concatenate 3 strings into an existing buffer, a very simple solution is:如果您需要将 3 个字符串连接到现有缓冲区中,一个非常简单的解决方案是:

char dest[DEST_SIZE];

snprintf(dest, sizeof dest, "%s%s%s", a, b, c);

Your str is local to your function.你的 str 对你的函数来说是本地的。
You could add a fourth parameter to your concatenated string or you could malloc it inside the function, just make sure to free it after use.您可以向连接的字符串中添加第四个参数,也可以在函数内部对其进行 malloc,只需确保在使用后将其释放即可。

char *concatenate(char *a, char *b, char *c)
{
  int size = strlen(a) + strlen(b) + strlen(c) + 1;
  char *str = malloc(size);
  strcpy (str, a);
  strcat (str, b);
  strcat (str, c); 

  return str;
}

int main(void) {

    char *str = concatenate("bla", "ble", "bli");

    printf("%s", str);
    free(str);

    return 0;
}

Maybe something like that:也许是这样的:

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

char * concatenate(const char *a, const char *b, const char *d)
{
    /* calculate the length of the new string */
    size_t len = strlen(a) + strlen(b) + strlen(d);
    /* allocate memory for the new string */
    char* str = malloc(len + 1);

    /* concatenate */
    strcpy(str, a);
    strcat(str, b);
    strcat(str, d); 

    /* return the pointer to the new string
     * NOTE: clients are responsible for releasing the allocated memory
     */
    return str;
}

int main(void)
{
    const char a[] = "lorem";
    const char b[] = "impsum";
    const char d[] = "dolor";
    char* str = concatenate(a, b, d);

    printf("%s\n", str);
    free(str);

    return 0;
}

如果您想要更通用的东西(例如连接 N 个字符串),您可以在此处查看 glib 库的 g_strconcat 的实现:https ://github.com/GNOME/glib/blob/master/glib/gstrfuncs。 c#L563

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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