編輯。是否有可能採用第二個函數並將源指針之後的最後一個元素作爲參數指向元素的指針?示例copy_ptrs(target3,source,source +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++);
}
}
賓果!感謝所有的幫助! –