Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指向一个元素?_C_Pointers - Fatal编程技术网

C 指向一个元素?

C 指向一个元素?,c,pointers,C,Pointers,编辑。是否可以使用第二个函数并将指向源后面最后一个元素后面的元素的指针作为参数?复制PTR示例(目标3、源、源+5) 我包含了整个程序的一个副本,但我正在尝试使用两个函数,一个使用数组表示法,另一个使用指针表示法来简单地复制用户读入的初始数组元素。但是,我的指针函数不起作用,因为它不会打印数据的副本。我认为我的指针声明是错误的?我对指针的了解非常有限,但我认为解决方案非常接近。任何帮助都会很好 #include <stdio.h> #define MAX 999 void copy

编辑。是否可以使用第二个函数并将指向源后面最后一个元素后面的元素的指针作为参数?复制PTR示例(目标3、源、源+5)

我包含了整个程序的一个副本,但我正在尝试使用两个函数,一个使用数组表示法,另一个使用指针表示法来简单地复制用户读入的初始数组元素。但是,我的指针函数不起作用,因为它不会打印数据的副本。我认为我的指针声明是错误的?我对指针的了解非常有限,但我认为解决方案非常接近。任何帮助都会很好

#include <stdio.h>
#define MAX 999

void copy_arr(double ar[], double ar2[], int n);
void copy_ptr(double arr[], double arr2[], int n);

int main() 
{
    int i, num; 
    double source[MAX];
    double target1[MAX];
    double target2[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);


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

    return 0;
}

void copy_arr(double target1[], double source[], int num)
{
    int i;

    for (i = 0; i < num; i++) 
    {
            target1[i] = source[i];
    }

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

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

}
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++;
    }
    q = target 2
    printf("\n\n***The second function uses pointer notation to copy the elements***\n");
    printf("===================================================================\n");

    for(i = 0; i < num; i++)
    {
            printf("\n              Pointer_Notation_Copy[%d] = %.2lf",i, *q++);
    }
}
#包括
#定义最大999
无效副本(双ar[],双ar2[],整数n);
无效副本(双arr[],双arr2[],整数n);
int main()
{
int i,num;
双源[最大值];
双目标1[最大值];
双目标2[最大值];
printf(“\n输入要读入数组的元素数:”;
scanf(“%d”和&num);
printf(“\n输入以下值(每次输入后按enter键)\n”);
对于(i=0;i
您正在使用
q
打印阵列
target2
q
for
循环的末尾不再指向它。在printf

    q = target2; 

要将指针
q
重置为pint数组
target2

您确实非常接近了

您只是忘记了在打印之前将
q
重置为
target2
;因此,在打印时,您试图显示超出数组末尾的元素

对于问题的“编辑”部分,您可以这样循环:

for (double *ptr = source ; ptr != pointer_after_last ; ++ptr)
使用此选项,您将使
ptr
一个接一个地指向每个元素。如果您想跟踪项目编号,只需添加一个变量,该变量在循环的每次运行中都会增加

程序的其余部分将完全相同。

在第二个函数中(在更新不应更新的帖子之前;否则reads将被混淆)当源数组的元素填充目标数组时,您正试图使用前面循环中已更改的指针输出目标数组的元素

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

您无法打印元素编号,因为
i
现在是undefined@Gianluca谢谢我甚至没有看到打印的内容。你能看看我的问题吗?我需要创建另一个函数,但完全卡住了。
void copy_ptr( double target2[], const double source[], int num )
{
    const double *p = source;
    double *q = target2;

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

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

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


}