C语言用指针拷贝数组的调用函数的顺序问题

这是一个用三个函数分别把source数组拷贝给三个target数组的代码,为什么现在这样运行target2打印出来是一堆随机的奇怪数字,但是把函数2和函数3的调用换一下位置,又运行正常了呢?


#include<stdio.h>
void copy_arr(double* t, double* s, int n);
void copy_ptr(double* t, double* s, int n);
void copy_ptrs(double* t, double* s, float* n);
int main(void)
{
    const double source[5] = { 1.1,2.2,3.3,4.4,5.5 };
    double target1[5];
    double target2[5];
    double target3[5];
    int index;
    copy_arr(target1, source, 5);     //函数1
    copy_ptr(target2, source, 5);     //函数2
    copy_ptrs(target3, source, source + 5);     //函数3

    printf("target1: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target1[index]);
    printf("\n\n");

    printf("target2: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target2[index]);
    printf("\n\n");

    printf("target3: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target3[index]);
    printf("\n\n");

    return 0;
}

void copy_arr(double* t, double* s, int n)
{
    int i;
    for (i = 0; i < n; i++)
        t[i] = s[i];
}

void copy_ptr(double* t, double* s, int n)
{
    for (int i = 0; i < n; i++)
        *(t + i) = *(s + i);
}

void copy_ptrs(double* t, double* s, float* n)
{
    int x, i;
    x = n - s;
    for (i = 0; i < x; i++)
        *(t + i) = *(s + i);
}

img



img

修改如下,改动处见注释,供参考:

#include<stdio.h>
void copy_arr(double* t, double* s, int n);
void copy_ptr(double* t, double* s, int n);
void copy_ptrs(double* t,double* s, double* n); //float* n 修改
int main(void)
{
    const double source[5] = { 1.1,2.2,3.3,4.4,5.5 };
    double target1[5];
    double target2[5];
    double target3[5];
    int index;
    copy_arr(target1, source, 5);     //函数1
    copy_ptr(target2, source, 5);     //函数2
    copy_ptrs(target3, source, source + 5);//函数3

    printf("target1: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target1[index]);
    printf("\n\n");

    printf("target2: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target2[index]);
    printf("\n\n");

    printf("target3: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target3[index]);
    printf("\n\n");
    return 0;
}

void copy_arr(double* t,double* s, int n)
{
    int i;
    for (i = 0; i < n; i++)
        t[i] = s[i];
}

void copy_ptr(double* t,double* s, int n)
{
    int i; // 修改
    for (i = 0; i < n; i++) // 修改
        *(t + i) = *(s + i);
}

void copy_ptrs(double* t,double* s, double* n) //float* n 修改
{
    int x, i;
    x = n - s;
    for (i = 0; i < x; i++)
        *(t + i) = *(s + i);
}

void copy_ptrs(double* t, double* s, int n)
{
    int i;
    for (i = 0; i < n; i++)
        *(t + i) = *(s + i);
}

【以下回答由 GPT 生成】

问题的答案: 在当前的代码中,target2会输出一些随机的奇怪数字的原因是由于函数3的调用出现了问题。函数3的参数中,第三个参数应该是长度n的指针,而在当前代码中,却传入了source + 5,即一个超过了source数组长度的指针。这导致了函数3在拷贝数组元素时访问越界,从而导致target2数组出现了随机的奇怪数字。

在当前代码中,函数2是先于函数3被调用的,因此函数2对于target2数组的拷贝操作在执行之前并未受到任何影响,所以target2数组的输出结果是随机的。而如果将函数2和函数3的调用顺序交换一下,即先调用函数3再调用函数2,那么函数3会先将target2数组指向source数组的指针进行拷贝,然后函数2再将source数组的内容拷贝到target2数组中。这样可以保证函数2在拷贝数组时所访问到的指针是有效的,因此target2数组的输出结果就是正确的了。

以下是修改后的代码,其中修正了函数3的参数传入错误的问题:

#include<stdio.h>

// 将源数组s拷贝到目标数组t,长度为n
void copy_arr(double* t, double* s, int n);

// 将源数组s指针拷贝到目标数组t,长度为n
void copy_ptr(double* t, double* s, int n);

// 将源数组s的指针拷贝到目标数组t的指针,长度为n
void copy_ptrs(double* t, double* s, double* n);

int main(void)
{
    const double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
    double target1[5];
    double target2[5];
    double target3[5];
    int index;

    // 使用函数1将source拷贝到target1
    copy_arr(target1, source, 5);

    // 使用函数3将source的指针拷贝到target3的指针
    copy_ptrs(target3, source, source + 5);

    // 使用函数2将source拷贝到target2
    copy_ptr(target2, source, 5);

    printf("target1: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target1[index]);
    printf("\n\n");

    printf("target2: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target2[index]);
    printf("\n\n");

    printf("target3: ");
    for (index = 0; index < 5; index++)
        printf("%5g", target3[index]);

    return 0;
}

void copy_arr(double* t, double* s, int n)
{
    int i;
    for (i = 0; i < n; i++)
        t[i] = s[i];
}

void copy_ptr(double* t, double* s, int n)
{
    int i;
    for (i = 0; i < n; i++)
        *(t + i) = *(s + i);
}

void copy_ptrs(double* t, double* s, double* n)
{
    while (s < n)
    {
        *t = *s;
        t++;
        s++;
    }
}

通过以上修改后的代码,可以保证函数2和函数3的调用顺序无论如何交换,都能够正确地拷贝source数组给target2数组,并输出正确的结果。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^