Dy_Xiu 2023-08-06 22:54 采纳率: 100%
浏览 9
已结题

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

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-08-06 23:27
    关注

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

    #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);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月15日
  • 已采纳回答 8月7日
  • 修改了问题 8月6日
  • 创建了问题 8月6日