【问题标题】:How to copy char array in C without inner function如何在没有内部函数的情况下在C中复制char数组
【发布时间】:2021-04-05 03:20:18
【问题描述】:

这是我的代码

char function(char *dst)
{
    int i;
    char *arr;

    i = 0;
    while(dst[i] != '\0')
    {
        arr[i] = dst[i];
        i++;
    }
    dst[i] != '\0'
    return(arr);
}

int main(void)
{
    char a[] ="asdf"
    printf("%s", function(a);
}

我想将*dst 复制到空*arr,但我的代码不起作用。 我无法理解。 如何在 C(ex_strcpy, memspy....) 中复制没有内部函数的数组 谢谢

【问题讨论】:

  • 我不明白你认为“内部功能”是什么意思,也不明白你为什么要避免它们。无论如何,您用于复制的任何功能都无法帮助您,因为问题是您没有正确理解底层内存中发生的事情。您不能随意选择某个地方来复制字符串to。你必须分配内存。
  • @JB P 提示:当arr[i]第一次被调用时,指针arr的值是多少?
  • 你的代码不会编译,除非你修复这个char a[] ="asdf"
  • char *arr; 未分配 arr[i] = dst[i] 将会大爆炸

标签: arrays c


【解决方案1】:

除了缺少; 并确保传递给函数的string 始终是'\0' 终止的(否则程序将遇到副作用strcpy 原因)。并返回char* 而不是char,您错过了为arr 分配内存

// return char * instead of char
char* function(char *dst)
{
    // Note - sizeof(dst) wont work
    // Neither does sizeof(dst)/sizeof(char)
    // allocate one extra for '\0'
    size_t size_to_alloc = (strlen(dst) + 1) * (sizeof *arr);
    char *arr = malloc( size_to_alloc  );
    char *p = arr;
    for ( ; *dst ; p++, dst++)
         *p = *dst;
    *p = '\0';
    return(arr);
} 

【讨论】:

    【解决方案2】:

    如果要动态复制数组,则需要使用 malloc 或其他等效方法为 char 数组分配内存。确保在完成后释放内存。我建议阅读一些关于 malloc 和在 c 中分配内存的帖子。

    这可能是一个不错的起点。

    https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/

    #include <stdio.h>
    #include <stdlib.h>
    
    char* function(char *dst, size_t length) {
        int i;
        // Allocating the memory needed for the char array.
        char *arr = (char*) malloc (sizeof(char) * length);
    
        i = 0;
        while(dst[i] != '\0') {
            arr[i] = dst[i];
            i++;
        }
        arr[length - 1] = '\0';
        return(arr);
    }
    
    int main(void) {
        char a[] ="asdf";
        // Getting length of the array
        size_t length = sizeof(a) / sizeof(a[0]);
        char* val = function(a, length);
        printf("%s", val);
        free(val);
    }
    
    

    【讨论】:

    【解决方案3】:

    您缺少内存分配,基本上是在尝试重新编码 strdup。见下文:

    char    *ft_strdup(const char *src)
    {
        char    *dst;
        int     len;
    
        len = 0;
        while (src[len]) // no inner function
            ++len;
        if (!(dst = malloc(sizeof(char) * (len + 1)))) // need 1 extra char to NULL terminate.
            return NULL;
        dst[len] = '\0';
        while (--len > -1)
            dst[len] = src[len];
        return dst;
    }
    

    请注意,编写您自己的 strdup 版本并将其作为 this function is not part of the C Standard 包含在您的程序库中是有意义的。

    【讨论】:

      【解决方案4】:

      如果有可能在不使用 c 函数的情况下复制字符串,也许可以通过 c 函数所做的事情来完成。

      看看 strcpy 做了什么可能会很有趣: https://code.woboq.org/userspace/glibc/string/strcpy.c.html

      char *
      STRCPY (char *dest, const char *src)
      {
        return memcpy (dest, src, strlen (src) + 1);
      }
      

      事实上它使用 memcpy:https://code.woboq.org/gcc/libgcc/memcpy.c.html

      这里是魔法......

      void *
      memcpy (void *dest, const void *src, size_t len)
      {
        char *d = dest;
        const char *s = src;
        while (len--)
          *d++ = *s++;
        return dest;
      }
      

      和 strlen:https://code.woboq.org/userspace/glibc/string/strlen.c.html

      【讨论】:

      • Minor: "without using c functions" --> 这个答案使用strlen()
      • 是的,但我们知道第一个字符串的长度。我添加了 strlen 源。谢谢
      【解决方案5】:

      您可以使用memcpy() 直接复制内存,例如在Memcpy, string and terminatorhttps://www.gnu.org/software/libc/manual/html_node/Copying-Strings-and-Arrays.html 在C 中,任何字符串都必须以\0(标记值)终止

      #include<stdio.h>
      #include<string.h>
      
      int main()
      {
        char source[] = "World";   
        char destination[] = "Hello ";   
        /* Printing destination string before memcpy */
        printf("Original String: %s\n", destination);
        /* Copies contents of source to destination */
        memcpy (destination, source, sizeof(source)); 
        /* Printing destination string after memcpy */
        printf("Modified String: %s\n", destination);
      
        return 0;
      }
      

      来源:https://www.educative.io/edpresso/c-copying-data-using-the-memcpy-function-in-c

      【讨论】:

        猜你喜欢
        • 2018-09-22
        • 1970-01-01
        • 2021-12-31
        • 2012-01-18
        • 2020-09-16
        • 2022-01-17
        • 1970-01-01
        • 2021-09-04
        • 2021-03-26
        相关资源
        最近更新 更多