gpt4 book ai didi

使用 c 将数组和指针复制到源的最后一个元素之后

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:41 25 4
gpt4 key购买 nike

是否可以初始化 double 组,然后将内容复制到另一个数组中。该程序应使用使用指针表示法的函数来复制原始源数组。

我应该使用的函数调用示例是 copy_ptrs(复制、源和指向源最后一个元素之后的元素的指针。)

这是我的主要引用

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;
}

这是我的简单复制指针符号函数

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++);
}
}

这是另一个使用数组表示法复制源数组的函数

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++);
}
}

当我尝试添加第三个函数来满足分配时,我卡住了。如何获取指向源最后一个元素之后的元素的指针?

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++);
}
}

最佳答案

您的陈述:

copy_ptrs(copy, source, and a pointer to the element following the last element of the source.)

和代码:

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

不匹配。

你来电

copy_ptrs(target3, source, source + num);

也不符合函数定义。

您可以将函数更改为:

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

使其与您的语句和函数调用相匹配。

然后,您必须将实现更改为:

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);
}
}

关于使用 c 将数组和指针复制到源的最后一个元素之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29994111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com