创建一个函数来复制C中类似strcpy的n个字符 #包括 #包括 无效nhap(字符*&scr,整数*n) { 做 { printf(“输入字符串长度:\n”); 扫描频率(“%d”,n); }n

创建一个函数来复制C中类似strcpy的n个字符 #包括 #包括 无效nhap(字符*&scr,整数*n) { 做 { printf(“输入字符串长度:\n”); 扫描频率(“%d”,n); }n,c,C,对不起,我有一个问题,我想创建一个类似strcpy的函数,但有些错误我自己无法修复。我想它会将n个元素从char*scr复制到char*dest,但当我运行代码时,它崩溃了。你能帮我修改代码并给我解释一下吗。我非常感谢。for循环应该是这样的 #include <stdio.h> #include <stdlib.h> void nhap(char* &scr, int *n) { do { printf("Input the st

对不起,我有一个问题,我想创建一个类似strcpy的函数,但有些错误我自己无法修复。我想它会将n个元素从char*scr复制到char*dest,但当我运行代码时,它崩溃了。你能帮我修改代码并给我解释一下吗。我非常感谢。

for循环应该是这样的

#include <stdio.h>
#include <stdlib.h>
void nhap(char* &scr, int *n)
{
    do
    {
        printf("Input the string length:\n");
        scanf_s("%d", n);
    } while (n < 0);

    scr = (char*)malloc(*n * sizeof(char));
    for (int i = 0; i < *n; i++)
    {
        scanf_s("%c", (scr + i));
    }
}

void xuat(char* scr, int n)
{
    printf("\nThe content of string: ");
    for (int i = 0; i < n; i++)
    {
        printf("%c", *(scr + i));
    }
}

char* StringNCopy(char* dest, char* scr, int n)
{
    if (n == NULL)
    {
        return NULL;
    }
    dest = (char*)realloc(dest, n * sizeof(char));
    for (int i = 0; i < n; i++)
    {
        for (int j = *(scr + n); j > 0; j--)
        {
            *(dest + i) = *(scr + j);
        }
    }
    *(dest + n) = '\0';
    return dest;
}


void main()
{

    char *a;
    char *b=NULL;
    int n;
    nhap(a, &n);
    xuat(a, n);
    StringNCopy(b, a, 4);
    printf("%s", *b);
    free(a);
}
for(int i=0;i
为此,不需要嵌套for循环,因为只需遍历数组一次并复制值。

修正程序

for (int i = 0; i < n; i++)
    {
        *(dest + i) = *(scr + i);
    }
#包括
#包括
无效nhap(字符*&scr,整数*n)
{
做
{
printf(“输入字符串长度:\n”);
scanf(“%d”,n);
}n<0;
scr=(char*)malloc((*n+1)*sizeof(char));//分配的大小应为n+1
fflush(stdin);
对于(int i=0;i<*n;i++)
{
scanf(“%c”,(scr+i));
}
}
无效xuat(字符*scr,整数n)
{
printf(“\n字符串的内容:”);
对于(int i=0;i

测试和工作正常。
观察评论中提到的错误

为什么不简单地使用string.h header的strncpy()函数?
{memcpy(dest,src,n);dest[n]=0;}
@ryker是的,我使用malloc作为char*scr@hecate这是我的练习,我必须创建一个新的,可能是我测试的副本。但是你知道,我想把n个元素从scr复制到dest,dest是空的。所以我让循环从0开始运行,但我不能用scr来运行。它崩溃并退出:DYour代码非常有用。它帮助我发现我没有尽全力去解决它,但我不能从char*改为void:D,但现在我可以用char*函数来解决它。谢谢你的建议。祝你有一个愉快的一天如果这篇文章是有帮助的,你可以把它标记为正确的,向上投票,使它对其他用户更有帮助
#include <stdio.h>
#include <stdlib.h>
void nhap(char* &scr, int *n)
{
    do
    {
        printf("Input the string length:\n");
        scanf("%d", n);
    } while (n < 0);

    scr = (char*)malloc((*n+1) * sizeof(char));    //allocated size should be n+1
    fflush(stdin);
    for (int i = 0; i < *n; i++)
    {
        scanf("%c", (scr+i ));
    }

}

void xuat(char* scr, int n)
{

    printf("\nThe content of string: ");
    for (int i = 0; i < n; i++)
    {
        printf("%c", *(scr + i));
    }
}

void StringNCopy(char* &dest, char* &scr, int n)     //no need to return the string aas you can pass it as reference
{

    if (n == NULL)
    {
        return;
    }

    dest = (char*)realloc(dest, (n+1) * sizeof(char));  //alloted size should be n+1
    for (int i = 0; i < n; i++)
    {

            *(dest + i) = *(scr + i);         //no need of nested loops

    }
    *(dest + n) = '\0';

}


int main()
{

    char *a;
    char *b=NULL;
    int n;
    nhap(a, &n);

    xuat(a, n);
    StringNCopy(b, a, 4);

    printf("\n6%s", b);
    free(a);
}