简体   繁体   English

使用c复制超出源最后一个元素的数组和指针

[英]copying arrays and pointers past the last element of the source using c

Would it be possible to initialize and array of double and then copy the contents into another array. 是否可以初始化double数组,然后将内容复制到另一个数组中。 The program should use a function using pointer notation to copy the original source array. 该程序应使用使用指针符号的函数来复制原始源数组。

Example of the function call I am supposed to use would be copy_ptrs(copy, source, and a pointer to the element following the last element of the source.) 我应该使用的函数调用示例为copy_ptrs(副本,源代码以及指向源代码最后一个元素之后的元素的指针。)

Here is my main for reference 这是我的主要参考

int main() 
{
    int i, num; 
    double source[MAX];
    double target1[MAX];
    double target2[MAX];
    double target3[MAX];


    printf("\nEnter number of elements to be read into the array: ");
    scanf("%d", &num);

    printf("\nEnter the values below (press enter after each entry)\n");

    for (i = 0; i < num; i++) 
    {
            scanf("%lf", &source[i]);
    }

    copy_arr(target1, source, num);
    copy_ptr(target2, source, num);
    copy_ptrs(target3, source, source + num);//This is how I was instructed to call the function.


    printf("\n\nCopying Complete!\n");

    return 0;
}

Here is my pointer notation function for a simple copy 这是我的指针符号函数,用于简单的复制

void copy_ptr(double target2[], double source[], int num)
{
    int i;
    double *p, *q;

    p = source;
    q = target2;

    for (i = 0; i < num; i++)
    {
            *q = *p;
        q++;
        p++;
    }

    printf("\n\n***The second function uses pointer notation to copy the elements***\n");
    printf("===================================================================\n");
    q = target2;

    for(i = 0; i < num; i++)
    {

        printf("\n              Pointer_Notation_Copy[%d] = %.2lf",i, *q++);
    }
}

Here is the other function to copy the source array using array notation 这是另一个使用数组符号复制源数组的函数

void copy_ptr(double target2[], double source[], int num)
{
    int i;
    double *p, *q;

    p = source;
    q = target2;

    for (i = 0; i < num; i++)
    {
            *q = *p;
        q++;
        p++;
    }

    printf("\n\n***The second function uses pointer notation to copy the elements***\n");
    printf("===================================================================\n");
    q = target2;

    for(i = 0; i < num; i++)
    {

        printf("\n              Pointer_Notation_Copy[%d] = %.2lf",i, *q++);
    }
}

When I try to add a 3rd function to satisfy the assignment I get stuck. 当我尝试添加第三个函数来满足分配要求时,我陷入了困境。 How do I take a pointer to the element following the last element of the source? 如何获取指向源最后一个元素之后的元素的指针?

void copy_ptrs(double target3[], double source[], int num)
{
    int i;
    double *p, *q;

    p = source;
    q = target3;

    for (i = 0; i < num; i++)
    {
            *q = *p;
        q++;
        p++;
    }

    printf("\n\n***The third function uses pointer notation to copy the elements + the number of elements read in?***\n");
    printf("===================================================================\n");
    q = target3;

    for(i = 0; i < num; i++)
    {

        printf("\n              Pointer_Notation_Copy[%d] = %.2lf",i, *q++);
    }
}

Your statment: 您的陈述:

copy_ptrs(copy, source, and a pointer to the element following the last element of the source.) copy_ptrs(副本,源代码以及指向源代码最后一个元素之后的元素的指针。)

and code: 和代码:

void copy_ptr(double target2[], double source[], int num)

don't match. 不匹配。

Your call 您的来电

copy_ptrs(target3, source, source + num);

does not match the function definition either. 也与功能定义不匹配。

You can change the function to: 您可以将功能更改为:

void copy_ptr(double target2[], double* source, double* end)

to make it match with your statement and function call. 使它与您的语句和函数调用相匹配。

Then, you'll have to change the implementation to: 然后,您必须将实现更改为:

void copy_ptr(double target[], double* source, double* end)
{
   int i = 0;
   int num = end - source;
   double* p = source;
   double* q = target;

   for ( ; p != end; ++p, ++q)
   {
      *q = *p;
   }

   printf("\n\n***The second function uses pointer notation to copy the elements***\n");
   printf("===================================================================\n");

   q = target;
   for ( i = 0; i < num; ++i, ++q )
   {
      printf("\n              Pointer_Notation_Copy[%d] = %.2lf", i, *q);
   }
}

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

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